2014-12-20 3 views
0

Я новичок в ROR. Я использую Rails-4 и Ruby 1.9.3. Я хочу вставить все записи студентов в базу данных. Пожалуйста, помогите написать фактический формат. Я таблица с именем «Студент» со следующими полямиКак вставить записи студентов в SQLite3 db с помощью ROR?

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. Какая правильная команда вставить все поля в данном формате или любой другой самый простой способ, пожалуйста, поделитесь со мной. Также отправьте фрагмент кода для получения всех значений на странице контроллера, если мой метод находится на странице контроллера после отправки.

def create 

end 

ответ

0

Команда рельсы генератор очень просто:

$ 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

Для qualitfication а hobbies вам придется добавить отдельные модели:

$ 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 %> 

Если вы хотите dinamycally добавить больше qualifications или hobbies Я рекомендую вам ознакомиться с номером nested_form gem от Ryan Bates, это вам очень поможет.

Надеюсь, это поможет!

Смежные вопросы