【发布时间】:2015-02-06 09:34:26
【问题描述】:
好的,这似乎是一个很常见的问题,但是在网上查找我的示例似乎找不到解决方案。
当尝试使用以下代码更新对象(产品)时,我得到 undefined method `stringify_keys' for "4":String NoMethod 错误页面被 Rails 抛出。
这里是代码,看看你是否能指出我做错了什么:
产品控制器
def update
@product = Product.find(params[:id])
if @product.update(params[:id])
redirect_to @product
else
render 'edit'
end
end
编辑表单视图:
<%= 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 %>
我希望我可以让它工作!
【问题讨论】:
-
这行
if @product.update(params[:id])是错的,应该是if @product.update(params[:product]) -
@product.update_attributes(params[:product]) -
@Pavan 不确定您是否愿意将其作为答案,以便我感谢您的帮助。很好的答案,非常感谢!
标签: ruby-on-rails