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’
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.
class ApplicationController < ActionController::Base
end
<% @user_interests.each do |interest| -%>
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.
class ApplicationController < ActionController::Base
�end
� �<% @user_interests.each do |interest| -%>
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.
So I figured out the issue, but now I am wondering if setting this
variable here isn’t a good idea, but rather doing it in a helper or
somewhere else?
I am using AuthenticatedSystem and it has a function current_user which
of cource gets the current user. So in my Application controller where
I am setting my @user_interests variable it some how doesn’t have access
to that function I guess. So as I created this function in the
ApplicationController and it all works now.
def cur_user
User.find(session[:user_id]) if session[:user_id]
end
So now my ApplicationController code looks like this:
class ApplicationController < ActionController::Base
include AuthenticatedSystem
helper :all # include all helpers, all the time
session :session_key => ‘_inchs_session’
See ActionController::RequestForgeryProtection for details
Uncomment the :secret if you’re not using the cookie session store