2012-03-19 59 views
0

在字符串存放CURRENT_USER得到CURRENT_USER最新的电子邮件后,他们提交评论试图从一个字符串

<%= f.text_field :commenter, :value => current_user, :readonly => "readonly" %> 

,所以我可以在电子邮件中我的评论观点

<td><%= comment.commenter.email %></td> 

但我'越来越

undefined method `email' for "#<User:0x7f26828>":String 

无论如何解决这个问题?

回答

3

你想存储整个对象在html输入?这应该不起作用,因为你只穿着字符串#<User:0x7f26828>。您应该使用关系模型之间:

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" %> 

并添加commenter_idComment模型。然后,当你创建只需设置commenter_id,一切都应该工作。

0

您是否在用户模型中使用了attr_accessible?

attr_accessible :email 
+0

错误我发现'String'类没有方法'email'。 '#'不是'User'类的对象,这只是字符串。 – 2012-03-19 11:46:38

+0

感谢您指出 – DavB 2012-03-19 13:36:43