使用belong_to的深层嵌套Rails表单不起作用? [英] Deeply nested Rails forms using belong_to not working?

查看:89
本文介绍了使用belong_to的深层嵌套Rails表单不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个杂乱的表单,除其他外,该表单必须管理具有两层嵌套的部分.它几乎可以正常工作,但是有一个障碍,我唯一能看到的与其他深层嵌套的工作形式不同的是,存在一个belongs_to关系而不是has_many.这是模型:

I'm working on a messy form which among other things has to manage a section with two-level nesting. It's almost working, but there's a snag and the only thing I can see that's different from other deeply-nested forms that work is that there's a belongs_to relationship rather than has_many. Here are the models:

Event
  has_many :company_events, :dependent => :destroy
  accepts_nested_attributes_for :company_events

CompanyEvent
  belongs_to :company
  accepts_nested_attributes_for :company, :update_only => true
  belongs_to :event
  belongs_to :event_type

Company
  has_many :company_events
  has_many :events, :through => :company_events

因此,通过链接表company_events,这是一个相当标准的多对多关系.所涉及的表单是使用动态的添加公司" Javascript按钮创建/编辑事件,这几乎都是基于Ryan Bates的截屏视频和GitHub存储库.

So it's a fairly standard many-to-many relationship via a link table, company_events. The form in question is creating/editing an event, with a dynamic "Add Company" Javascript button, all pretty much based on Ryan Bates' screencast and GitHub repo.

主要形式:

<table id="companies">
  <tr><th>Company Name</th></tr>
  <% f.fields_for :company_events do |builder| %>
    <%= render 'company_event_fields', :f => builder, :f_o => nil %>
  <% end -%>
</table>
<p><br/><%= link_to_add_fields "Add Company", f, :company_events, "events" %></p>

包括的形式如下.需要注意的重要一点是,公司ID是通过Javascript更新设置的,因为它很长,所以在此不再赘述.基本上,用户开始输入名称,显示自动完成列表,然后单击名称在表格中设置公司名称和ID.

And the included form is as follows. An important thing to note is that the company ID is set via a Javascript update, which I won't include here because it's long. Basically the user starts typing a name, an autocomplete list is displayed, and clicking on the name sets both the company name and the id in the form.

<tr class="company_event_fields">
  <td>
    <% f.fields_for(:company) do |company_form| -%>
      <%= company_form.text_field :name, :size => 80 %>
      <%= company_form.hidden_field :id %>
    <% end -%>
  </td>
  <td>
    <%= f.hidden_field :_destroy %>
    <%= link_to_function "remove", "remove_co_fields(this)" %>
  </td>
</tr>

当我更新现有记录时,一切正常.但是,当我尝试使用新创建的记录保存表单时,我得到:

When I update an existing record, everything works just fine. When I try to save the form with a newly-created record, though, I get:

ActiveRecord::RecordNotFound in EventsController#update

Couldn't find Company with ID=12345 for CompanyEvent with ID=

使用堆栈跟踪:

/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:401:in `raise_nested_attributes_record_not_found'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:289:in `assign_nested_attributes_for_one_to_one_association'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:244:in `company_attributes='
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `send'

我已经查看了nested_attributes中的代码,并使用调试器对其进行了运行.发生的事情似乎是因为有一个Company.id,所以ActiveRecord假定已经有一个条目,但是当然找不到.这似乎很奇怪,因为显然我需要传递一个ID才能创建一个新的CompanyEvent条目.所以,我想我丢失了一些东西.

I've looked through the code in nested_attributes, and run through it with a debugger. What's happening seems to be that because there's a Company.id, ActiveRecord is assuming that there is already an entry, but then of course it doesn't find one. This seems very odd, since obviously I'd need to pass in an ID in order to create a new CompanyEvent entry. So, I'm guessing that I'm missing something.

我发现的示例似乎全部都使用has_many关系嵌套了所有工作,而在这种情况下,这是一个Emirates_to,我想知道这是否是问题的根源.任何想法将不胜感激...

The examples I've found that work all seem to be nested using has_many relationships all the way down, while in this case it's a belongs_to, and I'm wondering if that's the root of the problem. Any ideas would be greatly appreciated...

推荐答案

这是我在类似问题中发布的另一种可能的解决方案: https ://stackoverflow.com/a/12064875/47185

Here's another possible solution that I posted in a similar question: https://stackoverflow.com/a/12064875/47185

类似这样的事情...

Something like this...

  accepts_nested_attributes_for :company
  def company_attributes=(attributes)
    if attributes['id'].present?
      self.company = Company.find(attributes['id'])
    end
    super
  end

这篇关于使用belong_to的深层嵌套Rails表单不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆