【发布时间】:2010-04-03 03:00:21
【问题描述】:
我有一个用户模型、一个帖子模型和一个兴趣模型。
User has_many posts through interests
User has_many interests
Post has_many users through interests
Post has_many interests
Interest belongs to Post
Interest belongs to User
Application_Controller如下:
class ApplicationController < ActionController::Base
before_filter :login_from_cookie
before_filter :find_user_interests
helper :all # include all helpers, all the time
session :session_key => '_blah_session'
include AuthenticatedSystem
def find_user_interests
@user_interests = current_user ? current_user.interests : []
true
end
end
Application.html.erb 有如下内容:
<%= render :partial => "users/interests", :object => @user_interests %>
_interests.html.erb 部分如下:
ul
<% unless current_user.nil? then -%>
<% @user_interests.each do |interest| -%>
li<%= interest.post.title %>/li
<% end %>
<% end -%>
/ul
考虑到这一切,当我在 localhost:3000/posts/1 时,我的部分显示正常,但在 localhost:3000/posts 时,我收到错误 undefined method 'title' for nil:NilClass,因此在上面显示的 li<%= interest.post.title %>/li 行中出现错误_interests.html.erb 部分。
这到底是什么问题?
TIA
【问题讨论】: