5

I am still struggling both writing the controller and the actual form to be able to nest one form in another with an optional model?

I have Message which has many contacts

When submitting a message, I want to add a contact optionally.

I have this as an example:

= simple_form_for Message.new, :remote => true do |f|
  #message_form
    = f.error_messages
    %p
      = f.input :account_name, :url => autocomplete_account_name_messages_path, :size => 40, :as => :autocomplete
    %p
      = f.input :topic, :required => true,
                :input_html => {:size => 30}

    #add_contact_btn
      = link_to "Add Contact"

      #contact_form
        = f.simple_fields_for :contactd do |fc|
        = fc.input :email
        = fc.input :first_name
        = fc.input :last_name

    = f.submit 'Give'
    = f.submit 'Request'

For Message.rb model, I have the following:

has_many :contacts
accepts_nested_attributes_for :contacts, :reject_if =>:all_blank

Note: When I used :contacts in the simple_fields_for it didn't work, so it is singular. But the reverse for accepts_nested_attributess_for.

In my create controller for message, I included message.contacts.build

But right now I am still generating nil contacts.

Here is what I see passed as form data from google chrome:

message%5Baccount_name%5D:McKesson
message%5Btopic%5D:testing a contact
message%5Bbody%5D:testing this
sender_id:
receiver_id:23
message%5Bcontacts%5D%5Bemail%5D:[email protected]
message%5Bcontacts%5D%5Bfirst_name%5D:Ang
message%5Bcontacts%5D%5Blast_name%5D:Name
2
  • What happens when I pull it out from the partial is that there are no input fields in the fc contact form
    – Satchel
    Jul 12, 2011 at 15:53
  • Show the code for the partial, and the render-line.
    – nathanvda
    Jul 15, 2011 at 5:54

4 Answers 4

8

The correct method name is simple_fields_for (notice the plural)

Also, you need to keep the f. to call it on the simple_form object

5
  • Hmm...I get an error when I put the 'f.' -- but I put it in a partial, would that affect it?
    – Satchel
    Jul 11, 2011 at 23:43
  • also, do I need to do anything in the controller so that the contact is created?
    – Satchel
    Jul 11, 2011 at 23:43
  • Yes, if you put it inside a partial, you need to pass the local variable along, for example render "fields", :f => f. Since you have accepts_nested_attributes_for in the model, it should work fine if you just call @message.save in the controller
    – axelarge
    Jul 13, 2011 at 15:22
  • ahhh.....thank you. I am getting closer to making this work, I hope. Let me give it a go and see....what types of associations will this work with? will it workw tih both belongs_to contact and has_many :contacts?
    – Satchel
    Jul 13, 2011 at 16:03
  • 1
    do you have suggestions on how to debug this...I still am not creating contacts....must be something to do with the accepts_nested_attributes_for........
    – Satchel
    Jul 15, 2011 at 4:05
4

I have a small project where I demonstrate how to use nested forms in simple-form, combined with cocoon (a gem I created to add/remove nested elements dynamically).

The project is on github.

Hope this helps.

1
  • thanks...I still don't get anything working -- no Contact is created :(
    – Satchel
    Jul 15, 2011 at 3:57
0

In my create controller for message, I included message.contacts.build

But right now I am still generating nil contacts.

Make sure you put in your Message.rb model the ability for it to accept the attributes too.

class Message < ActiveRecord::Base
    attr_accessible :contacts_attributes
    has_many :contacts
    accepts_nested_attributes_for :contacts

I know it doesn't answer your question fully but it may have just been this. When it comes to my project, it would return nil if i didn't include the :contacts_attributes, in my case it deals with products. Hope this helps even if I'm not using simple form as of now!

2
  • okay, let me try that...it's the first time I've seen it. If it were belongs_to :contact, would it be :contact_attributes?
    – Satchel
    Jul 20, 2011 at 5:02
  • I think it always has to be plural, so it has to be contacts_attributes. Did it work? Let me see your NEW and CREATE in your controller also. Jul 20, 2011 at 16:49
0

I faced similar issues working with nested forms. As suggested by JustinRoR you need to define attr_accessible: contacts_attributes.

You should be able to test the hash in the ruby console ( I am not sure if you have tried this). I suggest you to print the params[:message] and use this to create message from console like Message.new(params[:message]). (Note params[:message] is what you get by printing the params[:message] hash).

Once it works in console it should work like charm

4
  • thanks...what should the hash contain? Should it be message[contact][email]?
    – Satchel
    Jul 20, 2011 at 5:01
  • should look something like message =>{ account_name => "McKesson", topic => "testing a contact", body => "testing this", sender_id=> "", receiver_id => 23, "contacts_attributes"=>{ "0" => { email => [email protected], first_name => Ang, last_name => Name}}}. I am not sure how simple_forms deal with. Can you post logger.info(params[:message])'s output in your action and post it here so that we can inspect what simple form returns
    – naren
    Jul 20, 2011 at 17:45
  • I suggest you could try this f.simple_fields_for "message[contacts_attributes][]" do |fc| to achieve the above hash
    – naren
    Jul 20, 2011 at 18:22
  • thanks, let me try and I will repost the hash....I did not have contacts_attributes, before, it was just contacts => {}
    – Satchel
    Jul 21, 2011 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.