Recording current_user

Hi,

Another noob here I am afraid! I have a questuin about the current_user
method that comes with AAA.

I have AAA set up and working fine and if i put <%=
self.current_user.login %> into a view it shows the login name as
expected on the page.

What I cant work out is how to record this into the database when the
user completes a form.

My app so far only has two models. “user.rb” and “outlet.rb”… they
currently both have has_and_belongs_to_many set up.

When the user fills out the ‘new outlet’ form’ I want the current_user
's id to be stored in the column “user_id”, in the outlets table.

I am assuming I need to add something to my ‘create’ method in the
controller, but not sure exactly what.

Hope that is enough info. Thanks in advance,

3Quid

On 8/23/07, Graham D. [email protected] wrote:

What I cant work out is how to record this into the database when the
user completes a form.

My app so far only has two models. “user.rb” and “outlet.rb”… they
currently both have has_and_belongs_to_many set up.

When the user fills out the ‘new outlet’ form’ I want the current_user
's id to be stored in the column “user_id”, in the outlets table.

I am assuming I need to add something to my ‘create’ method in the
controller, but not sure exactly what.

there’s a few ways of doing this:

  1. outlet = Outlet.new(params[:outlet])
    outlet.user = current_user()
    outlet.save!

  2. @user = current_user()
    #returns a new object of the collection type that has been
    instantiated with attributes and linked to this object through the
    join table but has not yet been saved.
    @user.outlets.build(params[:outlet])
    @user.save!

  3. @user = current_user()
    #returns a new object of the collection type that has been
    instantiated with attributes and linked to this object through the
    join table
    @user.outlets.create(params[:outlet])

  4. @user = current_user()
    #adds one or more objects to the collection by creating associations
    in the join table
    @user.outlets << Outlet.new(params[:outlet])

see here for more info:

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000645

Adam

Hi Adam

Thanks for the reply,

I am afraid I am still not quite gettng this (stupid brain!) …

I tried the suggestions you gave me but all i seem to get is the
following error

“You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.outlets”

Any help would be most appreciated,

Thanks,

3quid

Adam C. wrote:

On 8/23/07, Graham D. [email protected] wrote:

What I cant work out is how to record this into the database when the
user completes a form.

My app so far only has two models. “user.rb” and “outlet.rb”… they
currently both have has_and_belongs_to_many set up.

When the user fills out the ‘new outlet’ form’ I want the current_user
's id to be stored in the column “user_id”, in the outlets table.

I am assuming I need to add something to my ‘create’ method in the
controller, but not sure exactly what.

there’s a few ways of doing this:

  1. outlet = Outlet.new(params[:outlet])
    outlet.user = current_user()
    outlet.save!

  2. @user = current_user()
    #returns a new object of the collection type that has been
    instantiated with attributes and linked to this object through the
    join table but has not yet been saved.
    @user.outlets.build(params[:outlet])
    @user.save!

  3. @user = current_user()
    #returns a new object of the collection type that has been
    instantiated with attributes and linked to this object through the
    join table
    @user.outlets.create(params[:outlet])

  4. @user = current_user()
    #adds one or more objects to the collection by creating associations
    in the join table
    @user.outlets << Outlet.new(params[:outlet])

see here for more info:

Peak Obsession

Adam

iOn 8/23/07, Graham D. [email protected] wrote:

“You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.outlets”

Any help would be most appreciated,

Thanks,

3quid

If you’re doing something like this:

@user = current_user()
@user.outlets.create(params[:outlet])

and you’re getting that error, it means that @user is nil. Make sure
that current_user() actually returns a valid user before attempting to
add outlets to them.

Adam