ruby - Unpermitted Parameters: profile (NestedAttributes) - RAILS 4 -
this question has many times asking others user still not solved problem here. im having problem rails apps error "unpermitted parameters: profile" appear.
user_controller.rb
class admin::userscontroller < applicationcontroller before_filter :set_user, only: [:edit, :update] before_filter :store_location, only: [:index] before_filter :require_admin def edit if @user render else redirect_to admin_users_path, notice: "user profile not found." end end def update # rails.logger.debug "===> (1)" if @user.update(user_params) redirect_to edit_admin_user_path, notice: "#{@user.profile.full_name} account has been updated." else render 'edit' end end private def set_user @user = user.find(params[:id]) end def user_params params.require(:user).permit(:id, :username, :email, profile_attributes: [:user_id, :full_name]) end end
edit.html.erb
<%= form_for :user, url: admin_user_path(@user), method: :patch |f| %> <div class="form-group"> <%= f.label :username %><br> <%= f.text_field :username, :class => "form-control" %> </div> <%= f.fields_for :profile, @user.profile |profile| %> <div class="form-group"> <%= profile.label :full_name %><br> <%= profile.text_field : full_name, :class => "form-control" %> </div> <% end %> <div class="form-group"> <%= f.submit "save", :class => "btn btn-primary" %> </div> <% end %>
user.rb
class user < activerecord::base has_one :profile accepts_nested_attributes_for :profile #, update_only: true, allow_destroy: true validates :username, :uniqueness => { :case_sensitive => false } end
profile.rb
class profile < activerecord::base belongs_to :user validates_presence_of :user_id validates_presence_of :full_name end
development.log
started patch "/master/users/7" 127.0.0.1 @ 2014-09-10 23:18:26 +0800 parameters: {"utf8"=>"✓", "authenticity_token"=>"23oufobayamcrfhw3r11f1x53ljat760shv0hqkmezw=", "user"=>{"username"=>"lisa", "profile"=>{"full_name"=>"evalisa andriaasdasda"}}, "commit"=>"save", "id"=>"7"} [1m[35muser load (0.3ms)[0m select `users`.* `users` `users`.`id` = 7 limit 1 [1m[36muser load (0.3ms)[0m [1mselect `users`.* `users` `users`.`id` = 6 order `users`.`id` asc limit 1[0m unpermitted parameters: profile [1m[35m (0.2ms)[0m begin [1m[36muser exists (0.4ms)[0m [1mselect 1 one `users` (`users`.`username` = 'lisa' , `users`.`id` != 7) limit 1[0m [1m[35m (0.2ms)[0m commit [1m[36mprofile load (0.4ms)[0m [1mselect `profiles`.* `profiles` `profiles`.`user_id` = 7 limit 1[0m
i don't know mistake. please help.
thanks in advance!
update user_params
method include of attributes profile object. missing id
rails trying create profile object , causes undesired behavior (your development log shows making patch request, trying update something, not create something):
def user_params params.require(:user).permit(:id, :username, :email, profile_attributes: [:id, :user_id, :full_name]) end
you may have update form pass id
of profile
object.
see answer look: https://stackoverflow.com/a/17561608/1026898
Comments
Post a Comment