Rails - Saving two related models through nested forms -
i have 2 models 1 user, , userprofile. userprofile belongs user, , user has 1 userprofile. when user signup 1 form collecting information , information goes 2 different models. cant seem achieve this. used accepts_nested_attributes_for :user_profile on user model. still user model gets saved not userprofile view:
<%= form_for(@user) |f| %> <%= render 'shared/error_messages' %> <%= f.label :name %> <%= f.text_field :name, class: 'form-control'%> <%= f.label :email %> <%= f.email_field :email, class: 'form-control'%> <%= f.label :password %> <%= f.password_field :password, class: 'form-control'%> <%= f.label :password_confirmation, "confirmation" %> <%= f.password_field :password_confirmation, class: 'form-control'%> <h3> profile information</h3> <%= f.fields_for @user.user_profile |profile_form| %> <%= profile_form.label :age %> <%= profile_form.text_field :age, class: 'form-control' %> <%= profile_form.label :height %> <%= profile_form.text_field :height, class: 'form-control' %> <%= profile_form.label :contact %> <%= profile_form.text_field :contact, class: 'form-control'%> <%= profile_form.label :city %> <%= profile_form.text_field :city, class: 'form-control'%> <%= profile_form.label :state %> <%= profile_form.text_field :state, class: 'form-control'%> <%= profile_form.label :batting_style %> <%= profile_form.text_field :batting_style, class: 'form-control'%> <%= profile_form.label :bowling_style %> <%= profile_form.text_field :bowling_style, class: 'form-control'%> <%= profile_form.label :favorite_players %> <%= profile_form.text_field :fav_players, class: 'form-control'%> <%= profile_form.label :favorite_team %> <%= profile_form.text_field :fav_team, class: 'form-control'%> <% end %> <br> <%= f.submit "create account", class: "btn btn-primary" %> <% end %> my users controller
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 email activate 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 application log:
started post "/users" ::1 @ 2015-03-31 14:38:58 -0400 processing userscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"tafzck9+l0ksbmsz8oeosqp5vbrfyfvz3rr5/tkkw3dr16+h2xr1m8itt+zjvigzpan4lichfhprix81lf6wna==", "user"=>{"name"=>"test user", "email"=>"testuser@gmail.com", "password"=>"[filtered]", "password_confirmation"=>"[filtered]", "user_profile"=>{"age"=>"29", "height"=>"5 feet 4 inches", "contact"=>"888-888-8888", "city"=>"philadelphia", "state"=>"pa", "batting_style"=>"right hand", "bowling_style"=>"right arm", "fav_players"=>"test", "fav_team"=>"test"}}, "commit"=>"create account"} unpermitted parameter: user_profile (0.1ms) begin transaction user exists (0.1ms) select 1 one "users" lower("users"."email") = lower('testuser@gmail.com') limit 1 sql (1.6ms) insert "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".* "user_profiles" "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) trying different ways: still no luck
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 email activate account." redirect_to root_url else render 'new' end end
Comments
Post a Comment