title: Rails 4.1 accepts_nested_attributes_for and has_one association with devise
body: <p>I'm trying to implement a dual registration (users can be either customers or providers) with Devise, but I'm getting nowhere (>_&lt;). </p>
  
  <p>So I have a sign up link with a parameter <a href="http://localhost:3000/users/sign_up?type=customer" rel="nofollow noreferrer">http://localhost:3000/users/sign_up?type=customer</a> and <a href="http://localhost:3000/users/sign_up?type=provider" rel="nofollow noreferrer">http://localhost:3000/users/sign_up?type=provider</a></p>
  
  <p>My problem is that if I put the nested form with <strong>:provider</strong> like <code>&lt;%= f.fields_for :provider do |fp| %&gt;</code> as I expect it to be, because is an <em>has_one</em> association it isn't shown. And if I put it with <strong>:providers</strong> like <code>&lt;%= f.fields_for :providers do |fp| %&gt;</code> the field is properly shown in the form, but it is not saved.</p>
  
  <p>I tried some of the proposed things in other posts (like <a href="https://stackoverflow.com/questions/7693929/rails-3-1-accepts-nested-attributes-for-and-has-one-association-wont-work">this</a> and <a href="https://stackoverflow.com/questions/17767449/rails-4-0-with-devise-nested-attributes-unpermited-parameters">this</a>), but nothing seems to work for me...</p>
  
  <p>Here is a simplified version of my code:</p>
  
  <p><strong>Routes:</strong></p>
  
  <pre><code>Rails.application.routes.draw do
    devise_for :users, :controllers =&gt; { registrations: 'registrations' }
    resources :users
    resources :customers
    resources :providers
  end
  </code></pre>
  
  <p><strong>Models:</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_one :customer
    accepts_nested_attributes_for :customer
  
    has_one :provider
    accepts_nested_attributes_for :provider
  end
  
  class Customer &lt; ActiveRecord::Base
    belongs_to :user 
  end
  
  class Provider &lt; ActiveRecord::Base
    belongs_to :user 
  end
  </code></pre>
  
  <p><strong>Controllers:</strong></p>
  
  <pre><code>class RegistrationsController &lt; Devise::RegistrationsController
    def sign_up_params
      params.require(resource_name).permit(:email, :password, :password_confirmation, customer: [:field_x], :provider: [:field_y]))
    end
  end
  </code></pre>
  
  <p><strong>Views:</strong></p>
  
  <pre><code>&lt;h2&gt;Sign up&lt;/h2&gt;
  &lt;%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %&gt;
    &lt;%= devise_error_messages! %&gt;
    ...
  
    &lt;% if params[:type] == "customer" %&gt;
      &lt;%= f.fields_for :customers do |fc| %&gt;
        &lt;div class="field"&gt;
          &lt;%= fc.label :field_x %&gt;&lt;br /&gt;
          &lt;%= fc.text_field :field_x, autofocus: true %&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  
    &lt;% if params[:type] == "provider" %&gt;
      &lt;%= f.fields_for :providers do |fp| %&gt;
        &lt;div class="field"&gt;
          &lt;%= fp.label :field_y %&gt;&lt;br /&gt;
          &lt;%= fp.text_field :field_y, autofocus: true %&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit "Sign up" %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Thanks a lot! </p>