一层:
form_tag @company do |f| ... end
两层
form_tag([@company, @company.departments.build]) do |f| ... end
这个…… @department下面假如还有@group, @group下面假如还有@employer, 这个 form_tag 应该是什么写法呢? 求答案,顺便求原理 谢谢大家
@department
@group
@employer
form_for @company do |f| = f.fields_for :departments do |d| = d.fields_for :members do |m|
具体看你情况来初始化这些对象。
可以直接在 form_tag 底下写 fields_for.例如 form_tag :company do = fields_for :departments do |d| = d.fields_for :group do |g|
@yakjuly @simonykq 如果我在后台直接Company.create(params[:company]),rails 会怎么处理?
Company.create(params[:company])
{:company=>{:departments=>{:group=>{...}}}} 这是后台得到的 Hash
{:company=>{:departments=>{:group=>{...}}}}
不好意思,应该是:
= form_tag "YOUR URL" do = fields_for :company do |c| = c.fields_for :departments do |d| = d.group do |g| ... end end end
后台得到的 hash 将是 {:company => {:departments => {:group => {}}}}
{:company => {:departments => {:group => {}}}}