Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 429
Peace Avatar asked Jun 08 '15 07:06

Peace


2 Answers

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
like image 182
makhan Avatar answered Sep 29 '22 06:09

makhan


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`])
like image 21
Nitin Rajan Avatar answered Sep 29 '22 06:09

Nitin Rajan