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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/23/07, Graham Dawe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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.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 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cohen wrote:> On 8/23/07, Graham Dawe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
iOn 8/23/07, Graham Dawe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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, > > 3quidIf 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 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- Action Controller Error: undefined local variable or method `current_user'
- using restful_authentication current_user inside controller specs
- acts_as_authenticated current_user in a model?
- Resyful authenticatio current_user in model
- Problem with undefined variable current_user