0

I'm trying to create a Provider record when a user signs up using devise. I have set it up as per: Devise: User belongs_to organization

however no provider is created. What could i be doing wrong?

User has_one Provider.
I modified my signup form:

<%= fields_for :provider do |provider_fields| %>
      <%= provider_fields.label :name, :label => 'Company Name' %>
      <%= provider_fields.text_field :name %>
 <% end %>

And my model

attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name,  :provider_attributes
    belongs_to :provider
     accepts_nested_attributes_for :provider

I can see in the rails s output:

 "provider"=>{"name"=>"kjhkjh"},

amongst the user attributes when i submit the form.

Community
  • 1
  • 1
Will
  • 4,498
  • 2
  • 38
  • 65

2 Answers2

1

Although I can't see the full user registration form to determine what block variable you are passing into the form builder, I'll assume it's |f|. If so, you need to prefix f to fields_for.

f.fields_for :provider

This way, you are passing :provider_attributes in your params, not simply provider. I see that you have already added :provider_attributes to attr_accessible (another common gotcha), so now you just need to be sure that is what your passing!

Source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

kries
  • 164
  • 1
  • 9
  • Thank you. When i make that change, the provider name field disappears altogether. Looking in browser tools, it isn't outputting anything at all when set like that. Might I have gotten something else wrong thats upsetting it? – Will Nov 28 '12 at 07:08
  • So googling around what is likely to cause that seems to be: <% needs to be <%= / use the right instance variable (provider_fields) / make sure accepts_nested_attributes_for :provider is set in the model. All of which i have done. Missing something. – Will Nov 28 '12 at 08:53
0

Ah ha! Self rescue. : )

Seems it's necessary to create the provider model if its nil so the helper knows what to make (or something)

so I added this to the model:

 def provider
    super || build_provider
  end

How to get devise to work with accepts_nested_attributes_for in a has one relationship?

Community
  • 1
  • 1
Will
  • 4,498
  • 2
  • 38
  • 65