【问题标题】:How to insert students records in SQLite3 db using ROR?如何使用 ROR 在 SQLite3 db 中插入学生记录?
【发布时间】:2014-12-20 07:18:00
【问题描述】:

我是 ROR 的新手。我正在使用 Rails-4 和 Ruby 1.9.3。我想将所有学生记录插入数据库。请帮助编写实际格式。我有一个名为“学生”的表,其中包含以下字段

1)FIRST NAME.
2)LAST NAME.
3)DATE OF BIRTH(format-dd-mm-yy,select from option).
4)EMAIL ID.
5)MOBILE NUMBER
6)GENDER.(select from radio button value)
7)ADDRESS 
8)CITY
9)PIN CODE
10)STATE
11)COUNTRY
12)HOBBIES (It takes from check box value)
13)QUALIFICATION.
  format:
   sl no   Examination  Board   Percentage   Year of Passing
     1     Class X      -         -           -
     2     Class XI     -         -           -       

14)COURSES APPLIED FOR(takes fron radio button select value)

我想运行命令rails g model Student FIRST NAME:string etc。以给定格式或任何其他最简单的方式插入所有字段的正确命令是什么,请与我分享。 如果提交后我的方法在控制器页面内,请分享一些代码 sn-p 用于检索控制器页面的所有值。

def create

end

【问题讨论】:

    标签: ruby-on-rails-3 ruby-on-rails-4 sqlite


    【解决方案1】:

    rails 生成器命令非常简单:

    $ rails g model Student first_name:string last_name:string date_of_birth:date email:string mobile_number:string gender:string address:string city:string pin_code:string state:string country:string courses_applied_for:string

    对于qualitficationhobbies,您必须添加单独的模型:

    $ rails g model Qualitification examination:string board:string percentage:decimal year_of_passing:string student_id:integer:index

    $ rails g Hobbie title:string student_id:integer:index

    student 模型应如下所示:

    class Student < ActiveRecord::Base
     has_many :hobbies
     has_many :qualifications
    
     accepts_nested_attributes_for :hobbies
     accepts_nested_attributes_for :qualifications
    end
    

    Hobbie 模型:

    class Hobbie < ActiveRecord::Base
     belongs_to :student
    end
    

    Qualification 模型:

    class Qualification < ActiveRecord::Base
     belongs_to :student
    end
    

    接下来是students_controller

    class StudentsController < ApplicationController
     def new
      @student = Student.new
     end
    
     def create
      @student = Student.new(student_params)
      if @student.save
       redirect_to root_path
       else
       render :new
      end
     end
    
     private
    
      def student_params
       params.require(:student).permit(:first_name, :last_name, :date_of_birth, :email, :mobile_number, :gender, :address, :city, :pin_code, :state, :country, :courses_applied, hobbies_attributes: [:title], qualifications_attributes: [:examination, :board, :percentage, :year_passing])
      end
    end
    

    最后表格应该如下所示,我将忽略学生字段,我将向您展示爱好和资格:

    <%= form_for @student do |f| %>
    .
    .
    .
     <%= f.fields_for :qualifications do |qf| %>
      <%= qf.text_field :examination %>
      <%= qf.text_field :board %>
      <%= qf.text_field :percentage %>
      <%= qf.text_field :year_of_passing %>
     <% end %>
     <%= f.fields_for :hobbies do |qf| %>
      <%= qf.text_field :title %>
     <% end %>
    <% end %>
    

    如果您想动态添加更多 qualificationshobbies,我建议您查看 Ryan Bates 的 nested_form gem,它将对您有很大帮助。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      相关资源
      最近更新 更多