Home > Database > Mysql Tutorial > How to develop a simple online questionnaire using MySQL and Ruby on Rails

How to develop a simple online questionnaire using MySQL and Ruby on Rails

王林
Release: 2023-09-21 11:24:30
Original
1283 people have browsed it

如何使用MySQL和Ruby on Rails开发一个简单的在线调查问卷

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.

  1. Environment setup

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
Copy after login

If the output shows the corresponding version number, it means the installation has been successful.

  1. Create database and tables

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
);
Copy after login

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.

  1. Create a Rails application

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
Copy after login

This will create a Create a new Rails app named "survey_app" and change into the app's directory.

  1. Set database configuration

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
Copy after login

Replace "your_mysql_password" with your MySQL password and make sure the database name is "survey_development".

  1. Generate model and controller

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
Copy after login

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
Copy after login

Open the "app/controllers/questions_controller.rb" file and add the following code:

class QuestionsController < ApplicationController
  def index
    @questions = Question.all
  end
end
Copy after login
  1. Add route

Open the "config/routes.rb" file and add the following code:

Rails.application.routes.draw do
  resources :questions, only: [:index]
  root 'questions#index'
end
Copy after login

This will define a root path named "questions#index".

  1. Create a view

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 %>
Copy after login

This will display the text of all questions.

  1. Run the application

Run the following command in the terminal to start the application:

rails server
Copy after login

Visit "http://localhost:3000" in the browser ", you will see a simple page showing the text of all questions.

  1. Add an issue

To add an issue to the database, you can use the Rails command line tool. Run the following command in the terminal:

rails console
Copy after login

Then run the following command to add a question:

Question.create(text: '你最喜欢的颜色是什么?')
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template