Ruby 1.9.3、Rails 3.2.13、Strong_parameters 0.2.1 の場合:
チュートリアルとレールキャストのすべての指示に従いましたが、strong_parameters が機能しません。それは本当に単純なもののはずですが、エラーがどこにあるのかわかりません。
config/initializers/strong_parameters.rb:
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
config/application.rb
config.active_record.whitelist_attributes = false
アプリ/モデル/product.rb
class Product < ActiveRecord::Base
end
app/controllers/products_controller.rb:
class ExpedientesController < ApplicationController
...
def create
@product = Product.new(params[:product])
if @product.save
redirect_to @product
else
render :new
end
end
end
これにより、予想どおり、禁止された属性の例外が発生します。しかし、次の場所に移動すると:
...
def create
@product = Product.new(product_params)
# and same flow than before
end
private
def product_params
params.require(:product).permit(:name)
end
次に、フォームに移動して「名前: 製品 1」と「色: 赤」を入力すると、例外は発生しません。新しい製品は色なしで正しい名前でデータベースに保存されます。
私は何を間違っていますか?