fields_for 不呈现关联名称(belongs_to 关系)

发布于 2024-11-16 13:59:36 字数 1826 浏览 5 评论 0原文

我正在尝试创建表单来创建事件。事件属于创建者(用户类),并且表单包括创建者的字段。

很大程度上,表单正在工作 - 它正确呈现其他所有内容,并且当 fields_for @event.creator 部分被注释掉时,创建操作可以正确处理它。不过, fields_for 标记正在呈现事件[用户]标记而不是事件[创建者],我不明白为什么。有想法吗?以下是相关摘录。

(在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 %>

(controllers/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

(models/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];当我提交表单时,我收到错误消息,该事件没有用户字段。

I'm trying to create the form to create an event. Events belong_to a creator (of class user), and the form includes fields_for the creator.

Largely the form is working - it renders everything else properly, and the create action processes it properly when the fields_for @event.creator section is commented out. The fields_for tag is rendering event[user] tags instead of event[creator], though, and i can't figure out why. Ideas? Here are the relevant excerpts.

(in views/events/new.html.erb)

<%= form_for @event do |form| %>
   <%= render :partial => '/events/form', :object => form %>
   <%= form.submit 'Go!' %>
<% end %>

(in 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 %>

(controllers/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

(models/event.rb)

belongs_to :creator, :class_name => "User"
accepts_nested_attributes_for :creator

Largely this works, but the form elements render as

<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>
...

the issue here is event_user and event[user] instead of event_creator and event[creator]; when i submit the form i get an error that event doesn't have a User field.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2024-11-23 13:59:36
<%= form.fields_for :creator, @event.creator do |uf| %>
<%= form.fields_for :creator, @event.creator do |uf| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文