title: Rails - Saving two related models through nested forms
body: <p>I have two models one User, and UserProfile. UserProfile belongs to User, and User has one UserProfile. When user signup it is one form collecting all the information and information goes into two different models. I cant seem to achieve this. I used accepts_nested_attributes_for :user_profile on user model. Still only User model gets saved but not the UserProfile
  My View:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>   &lt;%= form_for(@user) do |f| %&gt;
      &lt;%= render 'shared/error_messages' %&gt;
        &lt;%= f.label :name %&gt;
        &lt;%= f.text_field :name, class: 'form-control'%&gt;
  
        &lt;%= f.label :email %&gt;
        &lt;%= f.email_field :email, class: 'form-control'%&gt;
  
        &lt;%= f.label :password %&gt;
        &lt;%= f.password_field :password, class: 'form-control'%&gt;
  
        &lt;%= f.label :password_confirmation, "Confirmation" %&gt;
        &lt;%= f.password_field :password_confirmation, class: 'form-control'%&gt;
         &lt;h3&gt; Profile Information&lt;/h3&gt;
          &lt;%= f.fields_for @user.user_profile do |profile_form| %&gt;
              &lt;%= profile_form.label :age %&gt;
              &lt;%= profile_form.text_field :age, class: 'form-control' %&gt;
              &lt;%= profile_form.label :height %&gt;
              &lt;%= profile_form.text_field :height, class: 'form-control' %&gt;
              &lt;%= profile_form.label :contact %&gt;
              &lt;%= profile_form.text_field :contact, class: 'form-control'%&gt;
              &lt;%= profile_form.label :city %&gt;
              &lt;%= profile_form.text_field :city, class: 'form-control'%&gt;
              &lt;%= profile_form.label :state %&gt;
              &lt;%= profile_form.text_field :state, class: 'form-control'%&gt;
              &lt;%= profile_form.label :batting_style %&gt;
              &lt;%= profile_form.text_field :batting_style, class: 'form-control'%&gt;
              &lt;%= profile_form.label :bowling_style %&gt;
              &lt;%= profile_form.text_field :bowling_style, class: 'form-control'%&gt;
              &lt;%= profile_form.label :favorite_players %&gt;
              &lt;%= profile_form.text_field :fav_players, class: 'form-control'%&gt;
              &lt;%= profile_form.label :favorite_team %&gt;
              &lt;%= profile_form.text_field :fav_team, class: 'form-control'%&gt;
  
    &lt;% end %&gt;
          &lt;br&gt;
          &lt;%= f.submit "Create my account", class: "btn btn-primary" %&gt;
  
    &lt;% end %&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>My Users Controller</p>
  
  <pre><code>  def new
      @user = User.new
      @user_profile = @user.build_user_profile
    end
  
    def show
      @user = User.find(params[:id])
      @user_profile = @user.user_profile
    end
  
    def create
      @user = User.new(user_params)
  
      if @user.save
        @user.build_user_profile
        @user.send_activation_email
        flash[:info] = "Please check your email to activate your account."
        redirect_to root_url
      else
        render 'new'
      end
    end
  
    def user_params
       params.require(:user).permit(:name, :email, :password, :password_confirmation, user_profile_attributes: [:age, :contact, :height, :city, :state, :batting_style, :bowling_style, :fav_team, :fav_players])
    end
  </code></pre>
  
  <p>Application Log:</p>
  
  <pre><code>Started POST "/users" for ::1 at 2015-03-31 14:38:58 -0400
  Processing by UsersController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"tAFZCk9+l0ksbMSZ8OEosQP5VBRfYfvZ3RR5/tkkW3Dr16+h2Xr1m8ITt+zJvigZpaN4lIchFhPrIx81lf6wNA==", "user"=&gt;{"name"=&gt;"Test User", "email"=&gt;"testuser@gmail.com", "password"=&gt;"[FILTERED]", "password_confirmation"=&gt;"[FILTERED]", "user_profile"=&gt;{"age"=&gt;"29", "height"=&gt;"5 feet 4 inches", "contact"=&gt;"888-888-8888", "city"=&gt;"Philadelphia", "state"=&gt;"PA", "batting_style"=&gt;"Right hand", "bowling_style"=&gt;"Right Arm", "fav_players"=&gt;"Test", "fav_team"=&gt;"Test"}}, "commit"=&gt;"Create my account"}
  Unpermitted parameter: user_profile
     (0.1ms)  begin transaction
    User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('testuser@gmail.com') LIMIT 1
    SQL (1.6ms)  INSERT INTO "users" ("name", "email", "password_digest", "created_at", "updated_at", "activation_digest") VALUES (?, ?, ?, ?, ?, ?)  [["name", "Test User"], ["email", "testuser@gmail.com"], ["password_digest", "$2a$10$LsY8eVTzj0CNGUp1WLiZN.DOLRbU1Qyg8W/dUHDo77YmKH099.oOa"], ["created_at", "2015-03-31 18:38:58.699583"], ["updated_at", "2015-03-31 18:38:58.699583"], ["activation_digest", "$2a$10$y7iCb2vOJzTExisD.vkYzOWxs55W.FiYZ56aGYd028s49i1j9r3su"]]
     (0.8ms)  commit transaction
    UserProfile Load (0.5ms)  SELECT  "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = ? LIMIT 1  [["user_id", 120]]
    Rendered user_mailer/account_activation.html.erb within layouts/mailer (0.4ms)
    Rendered user_mailer/account_activation.text.erb within layouts/mailer (0.1ms)
  </code></pre>
  
  <p>Trying Different Ways: Still no luck</p>
  
  <pre><code>     def create
      @user = User.new(user_params)
      @user_profile = @user.build_user_profile(params[:user][:user_profile])
  
     if @user.save
        @user.send_activation_email
        flash[:info] = "Please check your email to activate your account."
        redirect_to root_url
     else
        render 'new'
     end
    end
  </code></pre>