On 2 April 2010 15:07, Jason Newport <[email protected]> wrote: > I have a User model, a Post model, and an Interest model: > User has_many posts through interests > User has_many interests, dependent destroy > Post has_many users through interests > has_many interests, dependent destroy > Post has_many interests > Interest belongs to Post > 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 > > 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? > > Also what is weird, if I take out the code in the partial that dislays > the title, so to get around this error, I have another issue. I have a > title in the partial that is set like "You have @user_interests.length > interests" and when I am on the posts page and say the posts page has 10 > posts my title reads like "You have 10 interests" so its counting all > posts as an interest, even if the user hasn't selected the post as an > interest. And when I go to one specific Posts page, on that single Post > page, the above referenced title reads "You have 1 interests" so its > seems to be counting the number of posts on the specific pages (index or > show) and totaling those up as a users interests. And if for one user I > add two interests and view the single post page then the title will read > "You have 3 interests", thus counts the two interests I really am > interested in plus the one for the Post page I am even though I have yet > to click/select it as an interest.
I would suggest installing ruby-debug, then you can break into the controller or the view and inspect the variables and work out what is going wrong. See the rails guide on debugging at http://guides.rubyonrails.org/ Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

