Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding nested attributes to devise user model
    primarykey
    data
    text
    <p>At first glance it would seem this question has been answered before, but not on my case (most are related to mass assignment issues and/or mongoid.) I have not been able to find an answer even though there are plenty somewhat related questions here.</p> <p>So basically I'm using the devise gem for user authentication on a ruby on rails application and I want to add a nested address model to the user model. I'm also using simple_form to generate the form views.</p> <p>I have a user model:</p> <pre><code>class User &lt; ActiveRecord::Base has_and_belongs_to_many :roles has_one :address accepts_nested_attributes_for :address, :allow_destroy =&gt; true end </code></pre> <p>This is the address model:</p> <pre><code>class Address &lt; ActiveRecord::Base belongs_to :country belongs_to :state belongs_to :user attr_accessible :street1, :street2, :country_id, :state_id, :city, :postalCode end </code></pre> <p>This is the Users controller, which I overrode in order to add a show action to display user profile.</p> <pre><code>class Users::RegistrationsController &lt; Devise::RegistrationsController def new resource = build_resource({}) resource.build_address respond_with resource end # POST /resource def create resource = build_resource(params[:user]) if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) respond_with resource, :location =&gt; after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location =&gt; after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource respond_with resource end end def show @user = User.find(params[:id]) #Add to view count if not viewing own user profile if @user != current_user @user.views += 1 @user.save end @vehicles = Vehicle.where(:user_id =&gt; @user) respond_to do |format| format.html # show.html.erb format.json { render json: @user } end end end </code></pre> <p>This is the view for creating new users:</p> <pre><code>&lt;%= simple_form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name) ) do |f| %&gt; &lt;fieldset&gt; &lt;%= f.input :username, :required =&gt; false, :autofocus =&gt; true, placeholder: 'Pick a username' %&gt; &lt;%= f.input :email, :required =&gt; false, placeholder: 'Your email' %&gt; &lt;%= f.input :password, :required =&gt; false, placeholder: 'Create a password' %&gt; &lt;%= f.input :password_confirmation, :required =&gt; false, placeholder: 'Confirm your password' %&gt; &lt;%= f.association :roles, :as =&gt; :check_boxes, :label =&gt; "", :required =&gt; false %&gt; &lt;%= f.simple_fields_for :address do |a| %&gt; &lt;%= a.input :street1 %&gt; &lt;%= a.input :street2 %&gt; &lt;%= a.association :country %&gt; &lt;%= a.association :state %&gt; &lt;%= a.input :city %&gt; &lt;%= a.input :postalCode %&gt; &lt;% end %&gt; &lt;%= f.button :submit, 'Sign up for free', :class =&gt; 'btn btn-success btn-large' %&gt; &lt;/fieldset&gt; &lt;% end %&gt; </code></pre> <p><strong>So the error upon creating a new user is this: Address user can't be blank</strong></p> <p>Basically the foreign key user_id is not being set for address.</p> <p>I was hoping somebody can shed a little light on this issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload