join - Rails: updating joined Active Model's attribute -


i'm new ror. question updating associated active model's attr.

class user   has_many :toys end  class toy   belongs_to :user end 

and have page form of user can update user's attributes, , attributes of associated user_devices:

<%= f.text_field :age %> # user <%= f.text_field :email %> # user .... <%= f.check_box :is_new %> # toy!! 

when post form , update attributes using update_attributes(), shows "activemodel::massassignmentsecurity::error"

@user.update_attributes(params[:user]) # gives activemodel::massassignmentsecurity::error 

another problem is, don't know how name "is_new" attribute it's in toy table.. should :toys_is_new?

i want associated toy's attributes updated well. can me this?

because is_new? toy, have use accepts_nested_attributes_for:

#app/models/user.rb class user < activerecord::base   has_many :toys   accepts_nested_attributes_for :toys end  #app/controllers/users_controller.rb class userscontroller < applicationcontroller   def edit     @user = user.find params[:id]   end    def update     @user = user.find params[:id]     @user.update user_params   end    private    def user_params     params.require(:user).permit(:age, :email, toys_attributes: [:is_new])   end end 

to work in view, you'll need use fields_for helper:

#app/views/users/edit.html.erb <%= form_for @user |f| %>   <%= f.fields_for :toys, @user.toys |t| %>     <%= t.object.name %>     <%= t.check_box :is_new %>   <% end %>   <%= f.submit %> <% end %> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -