Newbie problem associating user with created objects

I’m embarrassed that I can’t figure this out on my own. My only excuse
is that I’m just a few weeks into rails…

So, I’ve got restful_authentication all up a running fine. I’ve got an
“Idea” model, and all I want is for users to be associated with the
“ideas” they create. There’s no problem creating new ideas, but for
some reason I can’t get them associated with the user! When I try to
access @idea.user.login through the view, I get the error, “You have a
nil object when you didn’t expect it! The error occurred while
evaluating nil.login”

The relevant Idea Controller part:

  1. def create
  2. @idea = @current_user.ideas.build params[:idea]
    
  3. @idea.create_rating   ## I have a simple rating system
    

too…without this in there, the view freaks out if it tries display
the rating of an idea that has not yet been rated–I have vague sense
that this is what’s causing my current problem…
4.
5. respond_to do |format|
6. if @idea.save
7. flash[:notice] = ‘Idea was successfully created.’
8. format.html { redirect_to(@idea) }
9. format.xml { render :xml => @idea, :status
=> :created, :location => @idea }
10. else
11. format.html { render :action => “new” }
12. format.xml { render :xml => @idea.errors, :status
=> :unprocessable_entity }
13. end
14. end
15. end

The relevant view code is simple:

  1. <%= @idea.user.login %>

Some associations:
User:
has_many :ideas

Idea:
belongs_to :user
has_one :rating

Rating:
belongs_to :idea

A couple extra pieces of information: if this part of the view code is
removed, the view displays all other attributes of model just fine.
And here’s the kicker: if I go in rails console and create a new User,
‘u’, and then type in " i = u.ideas.build ‘, the console will spit out
the appropriate information when I type ’ i.user.login.’ Bah–why in
the console but not in the browser?! Can some more educated rails
developer help me out? Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs