Ruby on rails 正在尝试获取当前#u用户';来自字符串的最新电子邮件
在当前用户提交评论后将其存储在字符串中Ruby on rails 正在尝试获取当前#u用户';来自字符串的最新电子邮件,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,在当前用户提交评论后将其存储在字符串中 <%= f.text_field :commenter, :value => current_user, :readonly => "readonly" %> 当前用户:readonly=>“readonly”%> 所以我可以在我的评论视图中获取电子邮件 <td><%= comment.commenter.email %></td> 但是我越来越 undefined method `em
<%= f.text_field :commenter, :value => current_user, :readonly => "readonly" %>
当前用户:readonly=>“readonly”%>
所以我可以在我的评论视图中获取电子邮件
<td><%= comment.commenter.email %></td>
但是我越来越
undefined method `email' for "#<User:0x7f26828>":String
“#”的未定义方法“email”:字符串
是否要解决此问题?您是否在用户模型中使用了attr\u accessible
attr_accessible :email
您是否在您的用户模型中使用了attr_accessible
attr_accessible :email
是否要在html输入中存储整个对象?这不应该起作用,因为您只选择字符串
。
。您应该使用模型之间的关系:
class Commenter < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :commenter
end
类注释器
形式:
<%= f.text_field :commenter_id, :value => current_user.id, :readonly => "readonly" %>
current_user.id,:readonly=>“readonly”%>
并将
commenter\u id
列添加到Comment
模型中。然后,当您创建时,只需设置注释者id
,一切都应该正常。您想在html输入中存储整个对象吗?这不应该起作用,因为您只选择字符串。
。您应该使用模型之间的关系:
class Commenter < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :commenter
end
类注释器
形式:
<%= f.text_field :commenter_id, :value => current_user.id, :readonly => "readonly" %>
current_user.id,:readonly=>“readonly”%>
并将
commenter\u id
列添加到Comment
模型中。然后,当您创建时,只需设置commenter\u id
,一切都应该正常。在错误中,我看到classString
没有方法email
#
不是classUser
的对象,这只是字符串。我错误地看到classstring
没有方法email
#
不是类User
的对象,这只是字符串。