So yesterday I asked a question about getting the projects for a current user. Well I ran into an issue. See that thread here. So today I was working on displaying categories and tasks for the user along with any discussions. when I loaded up the page to view this I got:
NoMethodError in UsersController#show
undefined method `projects' for #<User:0xad5823f4>
Rails.root: /home/adam/Documents/Aptana Studio 3 Workspace/StartPoint
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:25:in `show'
Request
Parameters:
{"id"=>"4"}
This is currently whats in my users#show method:
def show
@projects = current_user.projects
@tasks = current_user.tasks
@categories = current_user.categories
@discussions = current_user.discussions
end
the table names are projects, tasks, categories and discussions. I have no problem using this any where else so why am I seeing this error?
if you need more code I can show it. the error is pretty clear but again I use this type of call else where to display project specific information such as a projects categories or tasks. could it be something with my relationship between a user and categories, discussions, tasks and so on? because I do have user_id in each table that is listed in the show method, among others.
Also I thought of this, this morning since were on this topic: current_user seems wrong because if I click on Bobs profile I will see all my stuff no? should it not be something more specific to the users id?
Have you established the association in each model?
Also be sure that
current_useris actually a User object and not some thin proxy.Generally you need to distinguish between the session user and the displayed user. This is why names like
current_userare harmful. I’d recommend usingsession_userto refer to the logged in user and@userto refer to the user being displayed.Some login systems define only a
current_usermethod, but to alias this isn’t hard. It’s just defective by design in that case.