2012-03-19 57 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