Railsquestion Zz
2010-Mar-31 07:52 UTC
How to assign currently logged in user name to a table field
I have 3 tables
items (columns are: id, name , type)
history(columns are: id, date, username, item_id, user_id)
user(id , username, password)
When a user "ABC" logs in and creates a new item, a history record
gets
created with the following after_create filter.
How to set the username field in history table to "ABC".
class Item < ActiveRecord::Base
has_many :histories
after_create :update_history
def update_history
histories.create(:date=>Time.now, username=> ?)
end
My login method in user_controller
def login
if request.post?
user=User.authenticate(params[:username])
if user
session[:user_id] =user.id
redirect_to( :action=>''home'')
flash[:message] = "Successfully logged in "
else
flash[:notice] = "Incorrect user/password combination"
redirect_to(:action=>"login")
end
end
end
I am not using any authentication plugin. I would appreciate if someone
could tell me how to achieve this without using plugin(like userstamp
etc.) if possible.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Hitesh Rawal
2010-Mar-31 12:17 UTC
Re: How to assign currently logged in user name to a table field
Hi, If u dont want to use any of the plugin then u need to write ur own action and need to do manually. Like: def current_user @current_user = User.find(session[:user_id]) end Keep this action into ur application controller, It will return u the current logedin user object. So u may use this like def update_history histories.create(:date=>Time.now, username=> current_user.name) end I hope it will work 4 u. But instead of doing manually I suggest u must use Authlogic gem/plugin it will gives u other benefits also. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.