Writing the user_id of user when creating an item


#1

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.


#2

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 C.
http://depixelate.com


#3

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


#4

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