1

Ok this seems a very common problem, but looking online I can't seem to find a solution for my example.

When trying to update an object(product) using the below code I get the undefined method `stringify_keys' for "4":String NoMethod error page thrown back by Rails.

Here is the code to see if you can point out what I'm doing wrong:

Product Controller

  def update
  @product = Product.find(params[:id])

  if @product.update(params[:id])
    redirect_to @product
  else
    render 'edit'
  end
end

The edit form View:

<%= form_for(@product.id) do |f| %>

  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <div class="field">
    <%= f.label :descriptio %><br>
    <%= f.text_field :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I'm hoping I can get this to work!

4
  • possible duplicate of undefined method `stringify_keys!' ruby on rails Commented Feb 6, 2015 at 9:37
  • 1
    This line if @product.update(params[:id]) is wrong.It should be if @product.update(params[:product]) Commented Feb 6, 2015 at 9:40
  • @product.update_attributes(params[:product]) Commented Feb 6, 2015 at 9:42
  • 1
    @Pavan unsure whether you'd like to place that as an answer so I can give you the credit for your help. Great answer, thanks a lot! Commented Feb 6, 2015 at 9:46

2 Answers 2

0

This fragment is incorrect.

def update
  @product = Product.find(params[:id])

  if @product.update(params[:id])
    redirect_to @product
  else
    render 'edit'
  end
end

update expects an Hash of attributes, you are passing a Fixnum ID. It should be

@product.update(params[:product])

Ideally, you should also filter the input, as requested by the strong parameters feature.

@product.update(product_params)

def product_params
  params.require(:product).permit(...)
end
Sign up to request clarification or add additional context in comments.

Comments

0

There are a couple of things wrong. Simone mentioned one in his answer.

If it is indeed as you posted, your edit form is incorrect.

<%= form_for(@product) do |f| %>   <-- no .id

The form is for an instance of the product model, not a FixNum.

The <% end %> right after your form-for closes that form.

The label for description won't match with the text-field.

Perhaps you intend:

<%= form_for(@product) do |f| %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.