[英]Rails 4 strong params - Undefined method name on model
I recently converted to Rails 4 and strong params, and I'm having a hard time figuring this out. 我最近改用了Rails 4和强大的参数,但现在很难弄清楚这一点。
My model looks like so: 我的模型如下所示:
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
validates :name, :email, :body, :presence => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
It's my understanding that I don't need a controller containing strong params for this model, because I'm strictly "getting" it. 据我了解,此模型不需要包含强参数的控制器,因为我严格地“获取”了它。 I'm not updating or saving it anywhere, so I don't need to permit any params to be passed through.
我不会在任何地方进行更新或保存,因此不需要允许任何参数传递。 However, to be sure, I have this controller:
但是,可以肯定的是,我有这个控制器:
class MessagesController < ApplicationController
private
def message_params
params.require(:message_params).permit(:name, :email, :body)
end
end
This throws an error: 这将引发错误:
message = Message.new
message.name = 'test'
NoMethodError: undefined method `name=' for #<Message:0x007f9d620706f0>
My model used to contain this: 我的模型曾经包含以下内容:
attr_accessor :name, :email, :body
And it worked fine. 而且效果很好。 What am I missing in my rails 4 upgrade, and what do I need to change to gain access to these attributes again?
我在Rails 4升级中缺少什么,需要进行哪些更改才能再次访问这些属性?
You still do need the attr_accessor
defining which attributes your model has. 您仍然需要
attr_accessor
定义模型具有的属性。
There's also a ActiveModel::Model
module you can include rather than including separate modules: 您还可以包括一个
ActiveModel::Model
模块,而不是单独的模块:
class Message
include ActiveModel::Model
attr_accessor :name, :email, :body
validates :name, :email, :body, :presence => true
def persisted?
false
end
end
See the ActiveModel documentation for more information. 有关更多信息,请参见ActiveModel文档 。
So you're still going to want to use attr_accessor. 因此,您仍然要使用attr_accessor。 You're mixing it up with attr_accessible.
您正在将其与attr_accessible混合在一起。
Take a look at this answer: Difference between attr_accessor and attr_accessible 看一下这个答案: attr_accessor和attr_accessible之间的区别
attr_accessor is "is a ruby method that makes a getter and a setter" while attr_accessible is now an unsupported "rails method that allows you to pass in values to a mass assignment: new(attrs) or up update_attributes(attrs)." attr_accessor是“是使获取器和设置器成为一个红宝石方法”,而attr_accessible现在是不受支持的“ rails方法,它允许您将值传递给大规模分配:new(attrs)或up_update_attributes(attrs)。”
You are creating a tableless model. 您正在创建一个无表模型。 The accessor methods are not created for your model by default.
默认情况下, 不会为您的模型创建访问器方法。 You need to add attr_accessor for the attributes of this specific model explicitly in order to read and write the attributes.
您需要为该特定模型的属性显式添加attr_accessor ,以便读写属性。 Hence, the error.
因此,错误。
For models with table, the model class extends from ActiveRecord::Base which takes care of creating the accessor methods for attributes dynamically so you don't specify attr_accesor there. 对于带有表的模型,模型类从ActiveRecord :: Base扩展而来,该类负责动态创建属性的访问器方法,因此您无需在此处指定attr_accesor 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.