Topic: Get ID of User (Using LoginGenerator)

Hello,

I am new in this forum and want to great for fist all of you.

I stated programming Rails applications some days ago and created a app with a restricted user area. For this I used the LoginGenerator.

Everything works perfect. But I have now a table where I want to write data and this table should contain the id of the user who is logged in.

users (id, login, passwd)  has_many ads (id, user_id, ...)

I am looking now for a way to get the user_id into these table.
I want to use a before_create in the model ad to fill up the colum ads.user_id with the data of the current connect user.

At moment I have no Idea how to realize this. From the session I got the connected user (users.login) but how can I come to the users.id?

Many thanks for your support
Mario

Re: Get ID of User (Using LoginGenerator)

Hi!

I'm newby too, but you might get an idea:

def create_ad
       ad = ad.new(:user_id => session["user"]["id"], :something1 = params["something1"]) # i'm not sure how is the right way.
       breakpoint() # use breakpoint to learn what's going on in you program*
   if ad.save
      flash[:notice] = 'Ad is saved!'
      redirect_to :action => 'main_url'
   else
      flash[:notice] = 'Ad was not saved!'   
      render :action => 'new'
   end
end

* I got a lot of info putting breakpoint() in code and and then run "ruby script/breakpointer". In console try: params, local_variable etc and you will see what you have and what you don't have.

Hope it helps.
Priit

Estonian ruby coders => http://ruby.ee