I'm trying to display a user's full name through the view template.
我正在尝试通过视图模板显示用户的全名。
<%= @status.user.full_name %>
Above code will give the undefined local variable or method `first_name'. I have set up the "full_name" method like this through devise gem generated user.rb model.
上面的代码将给出未定义的局部变量或方法`first_name'。我通过设计gem生成的user.rb模型设置了这样的“full_name”方法。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name,
:last_name, :profile_name
has_many :statuses
def full_name
first_name + " " + last_name
end
end
Lastly, first_name and last_name is defined through a devise migration file.
最后,first_name和last_name是通过设计迁移文件定义的。
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :profile_name
My guess is that attr_accessible causes an error with devise. Need your help! Thank you!
我的猜测是attr_accessible会导致设计错误。需要你的帮助!谢谢!
1 个解决方案
#1
0
Use self
to reference first_name
and last_name
of instance accessing them:
使用self引用访问它们的实例的first_name和last_name:
def full_name
self.first_name + " " + self.last_name
end
Update:
更新:
Thanks to OP for accepting the answer and also thanks to @JörgWMittag for pointing out how this answer was misleading in that the question was about Rails 4 and the originally suggested answer was assuming Rails 3.
感谢OP接受答案,并感谢@JörgWMittag指出这个答案是如何误导的,因为问题是关于Rails 4,最初建议的答案是假设Rails 3。
The solution for this specific problem would be to remove the attr_accessible
declaration from model altogether and use Strong Parameters. The suggested link which turned this answer to "Accepted Answer", I suppose, is Adding custom fields to your Devise User model in Rails.
这个特定问题的解决方案是从模型中完全删除attr_accessible声明并使用Strong Parameters。我想,建议的链接将此答案转为“已接受的答案”,即在Rails中为您的Devise用户模型添加自定义字段。
Thanks to @JörgWMittag again for pointing out the misuse of self
in the original answer. self
is not required on first_name
and last_name
in the full_name
method above. Had this method been a getter or setter then the use of self
keyword would have been necessary to imply that we want to operate on an instance's attribute and not a local variable in the function.
再次感谢@JörgWMittag指出在原始答案中滥用自我。上面的full_name方法中的first_name和last_name不需要self。如果这个方法是getter或setter那么使用self关键字就必须暗示我们想要操作实例的属性而不是函数中的局部变量。
There seem to be plenty of resources here in * regarding this topic but here is a well accepted answer with clear distinction: When to use 'self' in Ruby
*中似乎有很多关于这个主题的资源,但这里有一个明确区分的公认答案:何时在Ruby中使用'self'
#1
0
Use self
to reference first_name
and last_name
of instance accessing them:
使用self引用访问它们的实例的first_name和last_name:
def full_name
self.first_name + " " + self.last_name
end
Update:
更新:
Thanks to OP for accepting the answer and also thanks to @JörgWMittag for pointing out how this answer was misleading in that the question was about Rails 4 and the originally suggested answer was assuming Rails 3.
感谢OP接受答案,并感谢@JörgWMittag指出这个答案是如何误导的,因为问题是关于Rails 4,最初建议的答案是假设Rails 3。
The solution for this specific problem would be to remove the attr_accessible
declaration from model altogether and use Strong Parameters. The suggested link which turned this answer to "Accepted Answer", I suppose, is Adding custom fields to your Devise User model in Rails.
这个特定问题的解决方案是从模型中完全删除attr_accessible声明并使用Strong Parameters。我想,建议的链接将此答案转为“已接受的答案”,即在Rails中为您的Devise用户模型添加自定义字段。
Thanks to @JörgWMittag again for pointing out the misuse of self
in the original answer. self
is not required on first_name
and last_name
in the full_name
method above. Had this method been a getter or setter then the use of self
keyword would have been necessary to imply that we want to operate on an instance's attribute and not a local variable in the function.
再次感谢@JörgWMittag指出在原始答案中滥用自我。上面的full_name方法中的first_name和last_name不需要self。如果这个方法是getter或setter那么使用self关键字就必须暗示我们想要操作实例的属性而不是函数中的局部变量。
There seem to be plenty of resources here in * regarding this topic but here is a well accepted answer with clear distinction: When to use 'self' in Ruby
*中似乎有很多关于这个主题的资源,但这里有一个明确区分的公认答案:何时在Ruby中使用'self'