2017-08-23 53 views
1

我無法更新我的連接表course_studentsRails5 - 使用has_many更新連接表:通過:using fields_for

當我創建一個新來的學生,我想選擇從下拉列表中的課程(從兩個療程Course1Course2其已保存在課程表),並更新course_students。我希望我的couse_students表是這樣的。

id | student_id | course_id |   created_at   |   updated_at   
----+------------+-----------+----------------------------+---------------------------- 

    1 |   1 |   1 | 2017-08-23 16:41:57.228094 | 2017-08-23 16:41:57.228094 

感謝http://railscasts.com/episodes/17-habtm-checkboxes-revised?view=asciicast,我莫名其妙地發現了下面的代碼工作的radio_button_tag。但是當我將radio_button_tag更改爲select_tag時,出現錯誤wrong number of arguments

此外,我不確定這是否是正確的方式,因爲信息看起來有點老。經過一番谷歌搜索,我經常發現人們在new.html.erb中使用field_for,所以我試了一下,但是我無法更新course_students表。

今後,我想在course_students表中添加像teacher,date_from,class_room之類的其他列。

我真的很感謝你的幫助。

·機型

class Student < ApplicationRecord 
    has_many :course_students 
    has_many :courses, :through => :course_students 

    accepts_nested_attributes_for :course_students 
end 

class Course < ApplicationRecord 
    has_many :course_students 
    has_many :students, :through => :course_students 

end 

class CourseStudent < ApplicationRecord 
    belongs_to :student 
    belongs_to :course 

end 

·遷移

class CreateStudents < ActiveRecord::Migration[5.1] 
    def change 
    create_table :students do |t| 
     t.string :first_name 
     t.string :middle_name 
     t.string :last_name 

     t.timestamps 
    end 
    end 
end 

class CreateCourses < ActiveRecord::Migration[5.1] 
    def change 
    create_table :courses do |t| 
     t.string :name 

     t.timestamps 
    end 
    end 
end 

class CreateCourseStudents < ActiveRecord::Migration[5.1] 
    def change 
    create_table :course_students do |t| 
     t.integer :student_id 
     t.integer :course_id 

     t.timestamps 
    end 
    end 
end 

·課程表

id |  name  |   created_at   |   updated_at   
----+--------------+----------------------------+---------------------------- 
1 | Course1 | 2017-08-22 20:03:46.226893 | 2017-08-22 20:03:46.226893 
2 | Course2 | 2017-08-22 20:03:46.228765 | 2017-08-22 20:03:46.228765 

·student.controller,強PARAMS。

def student_params 
    params.require(:student).permit(:first_name, :middle_name, :last_name, course_ids: [], date_froms: []) 
    end 

·new.html.erb

<h1>Create new student</h1> 
<%= form_for(@student) do |f| %> 
    <%= f.label :first_name %> 
    <%= f.text_field :first_name %> 

    <%= f.label :middle_name %> 
    <%= f.text_field :middle_name %> 

    <%= f.label :last_name %> 
    <%= f.text_field :last_name %> 

    <%= Course.all.each do |course| %> 
     <%= radio_button_tag "student[course_ids][]", course.id, @student.course_ids.include?(course.id), id: dom_id(course)%> 
     <%= label_tag dom_id(course), course.name %> 
    <% end %> 

    <%= f.submit "Create new student", class: "btn btn-primary" %> 
<% end %> 

回答

1

使用nested_form_forform_for而不是和course_students使用field_for是嵌套形式助手提供。 https://github.com/ryanb/nested_form

<h1>Create new student</h1> 

<%= nested_form_for(@student) do |f| %> 
    <%= f.label :first_name %> 
    <%= f.text_field :first_name %> 

    <%= f.label :middle_name %> 
    <%= f.text_field :middle_name %> 

    <%= f.label :last_name %> 
    <%= f.text_field :last_name %> 

    <%= f.fields_for :course_students do |ff| %> 
    <%= ff.hidden_field :student_id, value: @student.id %> 
    <%= ff.collection_select :course_id, Course.all, :id, :name %> 
    <% end %> 

    <%= f.submit "Create new student", class: "btn btn-primary" %> 
<% end %> 

你需要允許course_students PARAMS在控制器這樣的:

def student_params 
    params.require(:student).permit(:first_name, :middle_name, :last_name, course_students_attributes: [ :course_id, :student_id]) 
end 
+0

非常感謝Ajinath先生。你的評論給了我線索!我發佈了我的代碼。 –

0

發現,下面的代碼工作!

students_controller

def new 
    @student = Student.new 
    @student.course_students.build 
    end 

    private 

    def student_params 
    params.require(:student).permit(:first_name, :middle_name, :last_name, course_students_attributes: [ :course_id, :student_id]) 
    end 

new.html.erb

<%= f.fields_for :course_students do |ff| %> 
    <%= ff.hidden_field :student_id, value: @student.id %> 
    <%= ff.collection_select :course_id, Course.all, :id, :name %> 
<% end %> 

謝謝!

相關問題