代表与设计用户不工作

时间:2012-03-08 03:05:32

标签: ruby-on-rails devise delegation

这就是我所拥有的。

item.rb的:

has_many :comments
belongs_to :user

user.rb:

devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model

attr_accessible :email, :password, :password_confirmation, :remember_me, :display_name
has_many :items 
has_many :comments, :through => :items

comment.rb:

belongs_to :item
belongs_to :user
delegate :display_name, :to => :user, :prefix => true

<%= comment.user_display_name%>正在返回

Comment#user_display_name delegated to user.display_name, but user is nil:
#<Comment id: 16, body: "fdsfsd", commenter_id: 1, item_id: 2, created_at:
"2012-03-07 23:41:10", updated_at: "2012-03-07 23:41:10">

1 个答案:

答案 0 :(得分:0)

您似乎在架构中有'commenter'而不是'user'。

也许你应该

belongs_to :commenter, :class_name => 'User', :foreign_key => 'user_id'

在你的评论中.rb