Ruby on rails Rails5-使用以下字段更新联接表:通过
我无法更新我的联接表Ruby on rails Rails5-使用以下字段更新联接表:通过,ruby-on-rails,Ruby On Rails,我无法更新我的联接表课程\u学生 创建新学员时,我想从下拉列表中选择一门课程(从已保存在课程表中的两门课程Course1和Course2),并更新course\u students。我希望我的couse_学生桌是这样的 id | student_id | course_id | created_at | updated_at ----+------------+-----------+----------------------
课程\u学生
创建新学员时,我想从下拉列表中选择一门课程(从已保存在课程表中的两门课程Course1
和Course2
),并更新course\u students
。我希望我的couse_学生桌是这样的
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
多亏了,我不知何故发现以下代码适用于单选按钮标签
。但是,当我将单选按钮\u标签
更改为选择标签
时,出现了一个错误
此外,我不确定这是否正确,因为信息似乎有点陈旧。在一些谷歌搜索之后,我经常发现人们在new.html.erb
中使用field\u作为
,所以我尝试了,但我无法更新课程学生
表
将来,我想在课程学生表中添加其他列,如教师、日期、教室等
我非常感谢你的帮助
・模型
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
・student.controller,强参数
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 %>
<%= 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 %>
创建新学生
对于使用嵌套表单,而不是表单,对于课程,学生使用嵌套表单助手提供的字段
发现以下代码有效
学生管理主任
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
<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 %>
<%= 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 %>
谢谢 非常感谢你,阿吉纳特先生。你的评论给了我线索!我发布了我的代码。
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
<%= 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 %>