我有一个用户模型、一个帖子模型和一个兴趣模型。
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 时,我收到错误,因此在 _interests.html.erb 部分中显示undefined method 'title' for nil:NilClass
的行 中出现错误。li<%= interest.post.title %>/li
到底会是什么问题?
TIA