Topic: Get ID of User (Using LoginGenerator)


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

Many thanks for your support

Re: Get ID of User (Using LoginGenerator)


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

def create_ad
       ad = => 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*
      flash[:notice] = 'Ad is saved!'
      redirect_to :action => 'main_url'
      flash[:notice] = 'Ad was not saved!'   
      render :action => 'new'

* 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.

Estonian ruby coders =>