Peace
Peace

Reputation: 626

undefined method `permit' for "51":String

This error is coming when I am updating, I don't know why this error is coming

def update1
 @user = User.find(params[:user])
 if @user.update_attributes(user_params)
   redirect to user_steps_path
 else
   render 'edit1'
 end
 end

private
  def user_params
    params.require(:user).permit(:fname, :lname, :email)
  end
end

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"wY+llV4qot5xDsxp1JvaltvPdrZND7goaorBTh8Vteo=",
 "user"=>"51",
 "commit"=>"Update"}

Edit.html.erb

This is my edit form

<%= form_for :user do |f| %>
    <div class="form-group">
    <%= f.label :First_Name %><br />
    <%= f.text_field :fname, :class=> 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :Last_Name %><br />
    <%= f.text_field :lname, :class=> 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :Email_Address %><br />
    <%= f.email_field :email, :class=> 'form-control' %>
  </div>
  <div class="button">
    <%= f.submit 'Update' %>
  </div>
</div>

<% end %>

please help me out on this...

Upvotes: 7

Views: 16535

Answers (4)

stevec
stevec

Reputation: 52468

This error can happen if you forgot "params" for example if you had this

def checkout_params
  permit(:property_id, :price_id)
end

instead of this

def checkout_params
  params.permit(:property_id, :price_id)
end

Upvotes: 0

user9869932
user9869932

Reputation: 7367

I got this solved by placing the :user key in the root level

{
 "user":
  {"utf8": "✓",
   "authenticity_token": "wY+llV4qot5xDsxp1JvaltvPdrZND7goaorBTh8Vteo=",
   "id": "51",
   "fname": "fname",
   "lname": "lname",
   "email": "email",
   "commit": "Update"
  }
}

Upvotes: 0

Nitin Rajan
Nitin Rajan

Reputation: 307

You have given same name(:user) in update1 method and in user_params method.that's why it is giving error

def update1
@user = User.find(params[`:user`])

private
   def user_params
    params.require(`:user`).permit(:fname, :lname, :email)
end

Try to change the :user(give some other name) in

@user = User.find(params[`:user`])

Upvotes: 5

makhan
makhan

Reputation: 4009

Your code doesn't make sense in the form it is in now:

@user = User.find(params[:user])
if @user.update_attributes(user_params)

In the first line you assume params[:user] equals to id. In the second you assume it's a hash of attributes.

Anyway to fix the problem you need to modify your user_params method, e.g.:

def user_params
  if params[:user].is_a? String
    params[:user]
  else
    params.require(:user).permit(:fname, :lname, :email)
  end
end

Or change the way you pass id:

params = {"utf8"=>"✓",
 "authenticity_token"=>"wY+llV4qot5xDsxp1JvaltvPdrZND7goaorBTh8Vteo=",
 "user"=>{ "id" => "51" },
 "commit"=>"Update"}

@user = User.find(params[:user][:id])

def user_params
  params.require(:user).permit(:id, :fname, :lname, :email)
end

Upvotes: 6

Related Questions