我是ror的新手。这是我的问题。 我使用generate scaffold命令来创建两个表部分和student.the列 section - name:string student - name:string section_id:integer
在模型中
section.rb
has_many :students
在模型中
student.rb
belongs_to :section
现在学生新表格有两栏要填写,即姓名和部分。要创建新学生,我是否必须知道学生所属的每个部分的ID?有没有其他方法可以获得我输入学生姓名和她的部分的表单?将学生表中的连接列更改为section_name有帮助吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
假设您的表单为您提供:name和:section_id,您可以在学生#create中执行此操作。
def create
@student = Student.create(:name => params[:name],
:section_id => params[:section_id])
end
答案 2 :(得分:0)
您无需更改表中的关联列名称。你的模型关联是正确的。
1)是的,您应该知道创建学生的每个部分的ID。 2)你的表格应该是
<%= f.collection_select(:section_id, @sections,:id,:name) %>
并且在学生控制器的创建动作中。
@student = Student.new(params[:students])
@student.save
这里@sections = Section.all