1

私は一種のオークション/寄付プロジェクトに取り組んでおり、製品の属性を変更したいと考えています。これは、製品が寄付されたことを示すブール値です。

私が今やっている方法は次のとおりです。

意見:

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

  <%= f.label "Are you sure you want to get this product?"%>
  <%= f.check_box :donated%>

  <%= f.submit "Receive!", class: "btn btn-large btn-primary" %>
 <% end %>

コントローラ:

   before_filter :signed_in_user, only: [:create, :destroy, :show, :index, :update]
   ...
   def update  
     @product.update_attributes(params[:product])
     flash[:success] = "Product donated!"
   end

ルート:

resources :products, only: [:show, :create, :new, :update]

そして、次のエラーが発生します。

undefined method `update_attributes' for nil:NilClass

app/controllers/products_controller.rb:30:in `update'
Request

Parameters:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"M9q/qVcDmVIlEx+T5VFF0YtkYtzHRUCZLkPkDjc7MJc=",
 "product"=>{"donated"=>"1"},
 "commit"=>"Update product",
 "id"=>"1"}

私は何を間違っていますか?ありがとうございます。

4

1 に答える 1

15

最初に既存の製品をロードしてから、その属性を更新する必要があります。

def update  
  @product = Product.find_by_id(params[:id])
  @product.update_attributes(params[:product])
  flash[:success] = "Product donated!"
end
于 2013-05-02T08:16:04.610 に答える