has_many through and partials

Posted by user307428 on Stack Overflow See other posts from Stack Overflow or by user307428
Published on 2010-04-02T04:36:06Z Indexed on 2010/04/02 4:43 UTC
Read the original article Hit count: 211

Filed under:
|

I have a User model, a Post model, and an Interest model.


Using User has_many posts through interests

Using User has_many interests

Using Post has_many users through interests

Using Post has_many interests

Using Interest belongs to Post

Using Interest belongs to User


Application_Controller is as follows:

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


Application.html.erb has as follows:

<%= render :partial => "users/interests", :object => @user_interests %>

_interests.html.erb partial is as follows:

ul <% unless current_user.nil? then -%>
<% @user_interests.each do |interest| -%> li<%= interest.post.title %>/li <% end %> <% end -%>
/ul


Given all this when I at localhost:3000/posts/1 my partial shows up fine, but when in localhost:3000/posts I get an error "undefined method `title' for nil:NilClass" thus an error in the line li<%= interest.post.title %>/li shown above in the _interests.html.erb partial.

What the heck would be the issue?

TIA

end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about partials