简体   繁体   English

接受belongs_to关联上的嵌套属性

[英]Accepting nested attributes on a belongs_to association

I have a complex form for scheduling events. 我有一个复杂的表格来安排事件。 Here are the abbreviated associations: 以下是缩写关联:

class Event < ActiveRecord::Base
  belongs_to :client
  accepts_nested_attributes_for :client, :reject_if => lambda { |a| a[:name].blank? }
end

class Client < ActiveRecord::Base
  has_many :events
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }
end

The form is creating a new event and I have the following structure: 表单正在创建一个新事件,我有以下结构:

- form_for @event do |event_form|
    %select=collection_select(client_options_for_select, :options, :group_name, :id, :name, @event.client_id)

   - event_form.fields_for :client do |client|
     = client.text_field :name

     - client.fields_for :questions do |question|
       = question.text_field :content

The client already exists and is chosen from a select menu. 客户端已存在,并从选择菜单中选择。 An observer renders the nested attributes form by setting the client variable in a controller action and then rendering the partial. 观察者通过在控制器操作中设置客户端变量然后渲染部分来呈现嵌套属性表单。

Here is the error I'm getting: 这是我得到的错误:

ActionView::TemplateError (wrong number of arguments (0 for 1)) on line #1 of app/views/proceedings/_questions.html.haml:
1: - event_form.fields_for :client do |client|

app/views/proceedings/_questions.html.haml:1:in `form'
app/views/proceedings/_questions.html.haml:1:in `_run_haml_app47views47events47_client_questions46html46haml_locals_client_questions_object'
haml (3.0.21) rails/./lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/proceedings_controller.rb:261:in `__instance_exec0'
app/controllers/proceedings_controller.rb:260:in `corp_client_questions'
app/controllers/proceedings_controller.rb:258:in `corp_client_questions'

I'm having problems (I think) with the belongs_to association between Event and Client. 我遇到问题(我认为)与Event和Client之间的belongs_to关联。 I don't know if Event can accept nested attributes of Client when the Event belongs_to the Client. 当事件属于客户端时,我不知道Event是否可以接受客户端的嵌套属性。 I've always done it the other way around (Client accepts nested attributes of Event). 我总是以相反的方式完成它(客户端接受Event的嵌套属性)。

Any ideas? 有任何想法吗? I can elaborate if you need more code or background. 如果您需要更多代码或背景,我可以详细说明。 Thanks! 谢谢!

Update: Added controller code as requested. 更新:根据请求添加了控制器代码。

def client_questions
if params[:client_id].blank?
  render_no_client_questions
elsif @client = Client.find(params[:client_id]) and @client.is_unspecified?
  render_no_client_questions
else
  respond_to do |format|
    format.js {
      render :update do |page|
        page[:client_questions].replace_html :partial => 'client_questions', :layout => false
      end
    }
  end
end

end 结束

try adding an instance of the fields_for object in the options... generally a symbol isn't enough when creating a new top-level form object... Try the following, but yes it is possible to accept nested attributes on a belongs_to. 尝试在选项中添加fields_for对象的实例...通常在创建新的顶级表单对象时,符号是不够的...尝试以下操作,但是可以接受belongs_to上的嵌套属性。

<%= event_form.fields_for :client, @client do |client| %>
  <%= client.text_field :name %>
  <%= client.fields_for :questions, Question.new do |question| %>
    <%= question.text_field :content %>
  <% end %>
<% end %>

I had the same issue using accepts_nested_attributes_for :address on a belongs_to :address association for my Order object. 我在订单对象的belongs_to:地址关联上使用accepts_nested_attributes_for :address时遇到了同样的问题。

The field_for wasn't echoing nothing, but all it took was adding @order.build_address() and that made it work. field_for没有回应任何东西,但它只需要添加@order.build_address()并使其工作。

I think is because we are using the association sort of reversed from the usual, so you have to manually create the association. 我认为是因为我们使用的关联类型与通常相反,因此您必须手动创建关联。

In your controller method(s), you need to add: 在您的控制器方法中,您需要添加:

@event.build_client

The form cannot display fields_for when it does not have a valid object. 当表单没有有效对象时,表单无法显示fields_for。 Most of the time, we use something like @event.client.build , but this will not work with a belongs_to association. 大多数情况下,我们使用类似@event.client.build东西,但这不适用于belongs_to关联。 That method is only valid with has_many and has_and_belongs_to_many. 该方法仅对has_many和has_and_belongs_to_many有效。

Reference here . 参考这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM