This question already has answers here:

Closed 4 years ago.

How is attr_accessible used in Rails 4?
(5个答案)
我是新手下面是我正在尝试的
class Category < ActiveRecord::Base
    attr_accessible :name
    has_many :post
end

但是我不知道如何在上面的scnerio中使用rails 4+中可访问的attr_。
我是照老办法做的。请建议

最佳答案

在Rails 4+中,没有质量分配支持。
你必须把控制器传过来然后像这样使用,

def create
  Category.create(category_params)
end

private

def category_params
  params.require(:category).permit(:name)
end

然后在控制器操作中调用category_params方法。

10-05 20:31
查看更多