Project

General

Profile

How can I get current user's name?

Added by Sergey Zuev about 14 years ago

I tried User.current.name. Didn't work although User.current.today works perfectly. Also tried user = User.find(User.current.id) and then user.name. Doesn't works also. Tried user = User.find(5) and then user.name (5 is an index of one of the users).
Tried it inside of one of the plugin's controllers.

What can be wrong?
Thanks in advance.


Replies (2)

RE: How can I get current user's name? - Added by Felix Schäfer about 14 years ago

Works fine for me in the script/console:

>> User.current.name
=> "Anonymous" 
>> User.find(3).name
=> "Felix Schäfer"

What error are you getting specifically? What does the trace in the logs show?

RE: How can I get current user's name? - Added by Sergey Zuev about 14 years ago

To tell the truth, I don't know as I'm a newbie in Ruby (I've been using it for only a week). The only thing I know is that this code doesn't return nil, I checked it by if statement.
Here is the piece of code I'm working on:
user = User.find(3)
if user.name
val = user.name # also tried user.name.to_s
elsif val = "not found!" #val gets assigned by "not found!"
end
cv2 = CustomValue.new :custom_field_id => cv.custom_field_id, :customized_id => cv.customized_id, :value => val, :customized_type => cv.customized_type

The problem is that new field wouldn't be created. When I put some random string value into val, it works fine. Do you have any ideas what can be the problem?
Could you please tell how can I know what errors I get and how to look at the trace in the logs?

P.S. Tried to google it, but everything I managed to find was some modules to debug ruby. Could you please give me a link to a tutorial or something like that?

Thanks in advance!

    (1-2/2)