我正在尝试创建表单来创建活动。事件属于(类用户的)创建者,表单包括创建者的fields_。
很大程度上,表单正在运行 - 它会正确呈现其他所有内容,并且当fields_for @ event.creator部分被注释掉时,create操作会正确处理它。尽管如此,fields_for标签是渲染事件[user]标签而不是event [creator],我无法弄清楚原因。想法?以下是相关的摘录。
(在views / events / new.html.erb中)
<%= form_for @event do |form| %>
<%= render :partial => '/events/form', :object => form %>
<%= form.submit 'Go!' %>
<% end %>
(在events / _form.html.erb中)
...
<% if !signed_in? %>
To create your event, please provide an email address and create a password. <br />
<%= form.fields_for @event.creator do |uf| %>
<div class="email_field">
<%= uf.label :email %>
<%= uf.text_field :email, :type => "email" %>
</div>
...
<% end %>
<% end %>
(控制器/ events_controller.rb)
def new
@event = Event.new
if !signed_in?
@event.build_creator
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @event }
end
end
(型号/ event.rb)
belongs_to :creator, :class_name => "User"
accepts_nested_attributes_for :creator
这很有用,但表单元素呈现为
<div class="email_field">
<label for="event_user_email">Email</label>
<input id="event_user_email" name="event[user][email]" size="30" type="email" />
</div>
<div class="password_field">
<label for="event_user_password">Password</label>
<input id="event_user_password" name="event[user][password]" size="30" type="password" />
</div>
...
这里的问题是event_user和event [user]而不是event_creator和event [creator];当我提交表单时,我收到错误,该事件没有用户字段。
答案 0 :(得分:3)
<%= form.fields_for :creator, @event.creator do |uf| %>