升级到rails4之后,像这这样的语句会报错:ActiveModel::ForbiddenAttributesError
@product = Product.new(params[:product])
方法是改为
@product = Product.new(product_params)
同时增加私有方法product_params.
class ProductsController < ApplicationController
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product, notice: 'Product was successfully created.'
else
render action: 'new'
end
end
private
def product_params
params.require(:product).permit(:name, .... :category)
end
end
讨论