Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 正在尝试获取当前#u用户';来自字符串的最新电子邮件_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

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
,一切都应该正常。

在错误中,我看到class
String
没有方法
email
#
不是class
User
的对象,这只是字符串。我错误地看到class
string
没有方法
email
#
不是类
User
的对象,这只是字符串。