Accepting nested attributes in belongs_to associations

I have a complex form for event planning. 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 creates 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 selected from the selection menu. The observer displays the form of the nested attributes, setting the client variable in the controller action and then providing a partial one.

Here is the error I get:

 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 am having problems (I think) with the belongs_to association between Event and Client. I do not know if the Event can accept the nested attributes of the Client when the Event belongs to the client. I always did it the other way around (the client accepts nested Event attributes).

Any ideas? I can develop if you need more code or background. Thanks!

Update: controller code added upon request.

 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

+4
source share
3 answers

try adding an instance of the fields_for object to the parameters ... Usually one character is not enough to create a new top-level form object ... Try the following, but yes, you can accept the nested attributes on a belongs to_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 %> 
+7
source

I had the same problem using accepts_nested_attributes_for :address in the assign_to: address association for my Order object.

Field_o there was nothing echo, but all that was needed was to add @order.build_address() and that made it work.

I think this is because we are using an association type that is canceled from normal, so you need to manually create an association.

+2
source

In your controller method (s) you need to add:

 @event.build_client 

A form cannot display fields if it does not have a valid object. In most cases, we use something like @event.client.build , but this will not work with the belongs_to association. This method is valid only with has_many and has_and_belongs_to_many.

Link here .

0
source

All Articles