How to develop a simple online questionnaire using MySQL and Ruby on Rails
With the popularity of the Internet, more and more people are beginning to use online questionnaires to Get opinions and feedback from users. Using MySQL and Ruby on Rails to develop a simple online questionnaire is a quick and efficient way. This article will introduce how to use these two tools to build a basic online questionnaire application and provide specific code examples.
First, make sure Ruby on Rails and MySQL are installed. Run the following command in the terminal to check:
ruby -v rails -v mysql -V
If the output shows the corresponding version number, it means the installation has been successful.
Create a database named "survey" in MySQL and create a table named "questions" in it. The table structure is as follows:
CREATE DATABASE survey; USE survey; CREATE TABLE questions ( id INT PRIMARY KEY AUTO_INCREMENT, text TEXT );
The table contains only two fields: id and text. id is the unique identifier of the record, and text stores the text of the question.
Go to the project directory in the terminal and run the following command to create a new Rails application:
rails new survey_app cd survey_app
This will create a Create a new Rails app named "survey_app" and change into the app's directory.
Configure the connection information of the MySQL database in the "config/database.yml" file. Add the following code block to the file:
development: adapter: mysql2 encoding: utf8 pool: 5 username: root password: your_mysql_password database: survey_development
Replace "your_mysql_password" with your MySQL password and make sure the database name is "survey_development".
Run the following command to generate a model named "Question" and the corresponding controller:
rails generate model Question text:text rails generate controller Questions index
This Corresponding files will be generated in the "app/models" and "app/controllers" directories. Open the "app/models/question.rb" file and add the following code:
class Question < ApplicationRecord end
Open the "app/controllers/questions_controller.rb" file and add the following code:
class QuestionsController < ApplicationController def index @questions = Question.all end end
Open the "config/routes.rb" file and add the following code:
Rails.application.routes.draw do resources :questions, only: [:index] root 'questions#index' end
This will define a root path named "questions#index".
Create a file named "index.html.erb" in the "app/views/questions" directory and add the following code:
<h1>调查问卷</h1> <% @questions.each do |question| %> <p><%= question.text %></p> <% end %>
This will display the text of all questions.
Run the following command in the terminal to start the application:
rails server
Visit "http://localhost:3000" in the browser ", you will see a simple page showing the text of all questions.
To add an issue to the database, you can use the Rails command line tool. Run the following command in the terminal:
rails console
Then run the following command to add a question:
Question.create(text: '你最喜欢的颜色是什么?')
Exit the command line tool and refresh the page in the browser, you will see the new addition The question appears in the list.
Summary
Through the above steps, we successfully developed a simple online questionnaire application using MySQL and Ruby on Rails. Use Rails to quickly build an application skeleton and use MySQL to store data. I hope this article can provide some guidance and help for beginners. In actual development, the application can also be expanded to add more functions and improve user experience.
The above is the detailed content of How to develop a simple online questionnaire using MySQL and Ruby on Rails. For more information, please follow other related articles on the PHP Chinese website!