我收到有关强参数的错误消息。我认为只是rails 4不再使用属性了。我的 toy.rb 的代码是:
class Toy < ActiveRecord::Base
attr_accessible :name, :price, :vendor
validates :name, :presence => true
validates :price, :presence => true
validates :price, :numericality => true
validates :vendor, :presence => true
end
如何将其更改为强参数?
编辑:我使用了不同的 rb,我将其更改为员工,这就是我所拥有的:
class Employee < ActiveRecord::Base
params.require(:employee).permit(:first, :last, :salary, :salary, :ssn)
validates :first, :presence => true
validates :last, :presence => true
validates :salary, :presence => true
validates :salary, :numericality => true
validates :ssn, :presence => true
结束
它仍然告诉我“# 的未定义局部变量或方法‘params’”
最佳答案
您需要的代码是
params.require(:toy).permit(:name, :price, :vendor)
您将把它放入您的 Controller 中。通常,您创建一个私有(private)方法:
def create
Toy.create(toy_params)
end
private
def toy_params
params.require(:toy).permit(:name, :price, :vendor)
end
参见http://guides.rubyonrails.org/getting_started.html#saving-data-in-the-controller了解更多信息。
编辑 我想我原来的答案可能误导了你。代码位于 Controller 中,而不是模型中。
关于ruby-on-rails - Ruby 中的强参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21590718/