How to develop a simple message board function using MySQL and Ruby on Rails

WBOY
Release: 2023-09-20 13:00:18
Original
931 people have browsed it

如何使用MySQL和Ruby on Rails开发一个简单的留言板功能

How to develop a simple message board function using MySQL and Ruby on Rails

The message board is a very common website feature that allows users to post messages and communicate with other people to interact and discuss. In this article, I'll show you how to develop a simple message board functionality using MySQL and Ruby on Rails.

Preparation
First, we need to make sure you have installed the MySQL database and Ruby on Rails framework. You can download and install them from the following link:
MySQL: https://www.mysql.com/downloads/
Ruby on Rails: https://rubyonrails.org/

Create a Rails application
First, we need to use the Rails command line tool to create a new Rails application. Open a terminal and run the following command:

rails new message_board cd message_board
Copy after login

This command will create a new Rails application named message_board and enter that directory.

Set up the database connection
Next, we need to set up the connection between the Rails application and the MySQL database. In the config/database.yml file, find the development section and modify it to the following:

development: adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: your_mysql_username password: your_mysql_password database: message_board_development host: localhost
Copy after login

You need to replace your_mysql_username and your_mysql_password with your MySQL database username and password.

Create Message Board Model
Now we can start creating our message board model. Run the following command to generate a model named Message:

rails generate model Message content:text
Copy after login

This will generate a file named message.rb in the app/models directory and a migration file in the db/migrate directory. Open the generated migration file (file name with timestamp) and add a column to store the content of the message:

class CreateMessages < ActiveRecord::Migration[6.0] def change create_table :messages do |t| t.text :content t.timestamps end end end
Copy after login

Run the database migration command to create the messages table:

rails db:migrate
Copy after login

Create Message-related controllers and views

Next, we need to generate a controller and view to handle message-related requests. Run the following command to generate a controller named Messages:

rails generate controller Messages
Copy after login

This will generate a file named messages_controller.rb in the app/controllers directory and a file named messages in the app/views directory folder.

Open the messages_controller.rb file and add the following methods:

class MessagesController < ApplicationController def index @messages = Message.all end def create @message = Message.new(message_params) if @message.save redirect_to messages_path else render :index end end private def message_params params.require(:message).permit(:content) end end
Copy after login

Two methods, index and create, are defined here. The index method is used to display all messages, and the create method is used to create new messages. . We also need to add routes in the routes.rb file:

Rails.application.routes.draw do resources :messages, only: [:index, :create] root to: "messages#index" end
Copy after login

In this way, our controller and routing are set up. Now we can modify the messages/index.html.erb file to create the front-end interface of the message board.

留言板

<% if @messages.any? %> <% @messages.each do |message| %>
<%= message.content %>
<% end %> <% else %>
尚无留言
<% end %> <%= form_with model: Message.new, url: messages_path do |form| %> <%= form.text_area :content %> <%= form.submit "发表留言" %> <% end %>
Copy after login

Here, we use a simple loop to loop through all messages and display them on the interface. We also use a form to create and submit new comments.

So far, we have completed the development of a simple message board function. You can run the Rails server and visit http://localhost:3000 in the browser to see the effect:

rails server
Copy after login

Summary
It is not complicated to develop a simple message board function using MySQL and Ruby on Rails . We accomplished this by generating models, controllers, and views, taking advantage of the convenience of the Rails framework to simplify the development process. I hope this article can provide you with some useful guidance and inspiration.

The above is the detailed content of How to develop a simple message board function using MySQL and Ruby on Rails. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!