title: Rails Devise - Register User with Associated Model
body: <p>I've come across a few SO questions on this topic, but all seem <a href="https://stackoverflow.com/questions/4307743/profile-model-for-devise-users">out of date</a> or simply bad coding practice.</p>
  
  <p>Problem: I am signing up a user as part of a checkout flow. I want to collect the user's address when they sign up.  I have a user model and an address model. I can't seem to figure out how to properly override Devise's registration's controller to allow the additional params.</p>
  
  <p>Here's what I've started with:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_many :orders
    has_many :addresses, :dependent =&gt; :destroy
  
    accepts_nested_attributes_for :addresses
  
  end
  </code></pre>
  
  <p>I've also got my address model:</p>
  
  <pre><code>class Address &lt; ActiveRecord::Base
    belongs_to :state
    belongs_to :user
  end
  </code></pre>
  
  <p>...in routes.rb:</p>
  
  <pre><code>   devise_for :users, controllers: {registrations: 'registrations'}
  </code></pre>
  
  <p>And finally, my attempt at overriding the devise registration's controller:</p>
  
  <pre><code>class RegistrationsController &lt; Devise::RegistrationsController
  
    before_filter :configure_permitted_parameters
  
    # GET /users/sign_up
    def new
  
      # Override Devise default behaviour and create a profile as well
      build_resource({})
      resource.build_address
      respond_with self.resource
    end
  
    protected
  
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u|
        u.permit(:email, :password, :password_confirmation, :address_attributes =&gt; [:address, :address2, :city, :state_id, :zip_code])
      }
    end
  end
  </code></pre>