Forum: Ruby on Rails writing the user_id of user when creating an item

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F472f7a1e0ddd9577d1a331bda710dc8?d=identicon&s=25 Pleigh Ful (pleigh)
on 2007-03-19 03:03
hi there, i have this problem, i am a newb in ror and don't know how to
do this. i have a user_id column in a database table named items, i also
use the login generator, i can signup, login, and logout, no problem
with it, but when i am logon to the system, and tried to make a new
item, the user_id field is null, my user_id is not written in the
user_id table, and i have really no idea how to do it. i have
associations, has_many :items in user.rb model, and belongs_to :user in
item.rb.

i know you can help me with this, like i've said, i am just learning ror
and new to this forum as well. please post your replies on what info you
need to help me.

thanks in advance.
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-03-19 06:21
(Received via mailing list)
You likely want to do something like this is your controller:

current_user.items.create(params[:item])

This will automatically set the user_id for you.

--
Zack Chandler
http://depixelate.com
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2007-03-20 06:33
Another option would be to store the user_id in session[:user_id] during
login.  Then when you create a new object:

unless session[:user_id].empty?
  @thing = Thing.new(params)
  @thing.user_id = session[:user_id]
  @thing.save
else
  flash[:notice] = "Log in before creating something!"
  redirect_to :somewhere
end
97822aeb6cea1c6b6e6a7170c6e96fd8?d=identicon&s=25 Mike Gilbert (Guest)
on 2007-03-20 10:43
(Received via mailing list)
Pleigh Ful wrote:
> and new to this forum as well. please post your replies on what info you
> need to help me.
>
> thanks in advance.
>
>
Try the UserStamp plugin: http://delynnberry.com/projects/userstamp/

HTH

Mike
This topic is locked and can not be replied to.