Ruby Forum Ruby on Rails > Find(:user_id)

Posted by Joe Sutherland (joeturtle)
on 19.08.2008 20:30
I'm trying to get the current user by using the find method from the
User object.
I'm using restful authentication to handle session management.

The login page redirects to the user page, on successful log in, whose
code looks like this:

def userpage
    login_required
    @user = User.find(:user_id)
end

However, I get the following error when I view the page:

PGError: ERROR:  invalid input syntax for integer: "--- :user_id
"
: SELECT * FROM "users"     WHERE ("users"."id" = E'--- :user_id
')

Any help is appreciated.

If it's not clear what I'm trying to do, I'm trying to get the current
user and then show all the associated records for that user. I haven't
gotten to the second part yet as the first has proven beyond my limited
abilities!
Posted by Philip Hallstrom (Guest)
on 19.08.2008 20:36
(Received via mailing list)
On Aug 19, 2008, at 11:30 AM, Joe Sutherland wrote:

>    @user = User.find(:user_id)
>
I'm guessing you want...

User.find(params[:user_id])

or maybe

User.find(params[:id])
Posted by Jodi Showers (jshow)
on 19.08.2008 20:37
(Received via mailing list)
Hey Joe -

On 19-Aug-08, at 2:30 PM, Joe Sutherland wrote:

>    @user = User.find(:user_id)
>
> If it's not clear what I'm trying to do, I'm trying to get the current
> user and then show all the associated records for that user. I haven't
> gotten to the second part yet as the first has proven beyond my  
> limited
> abilities!

if the user is logged in, and you have the has_many
setup(has_many :records) correctly, then current_user will all you need.

before_filter :login_required, :only => :userpage

def userpage
   @records = current_user.records
end

Jodi
Posted by Frederick Cheung (Guest)
on 19.08.2008 20:39
(Received via mailing list)
On Aug 19, 7:30 pm, Joe Sutherland <rails-mailing-l...@andreas-s.net>
wrote:
> end
>
> However, I get the following error when I view the page:
>
> PGError: ERROR:  invalid input syntax for integer: "--- :user_id
> "
> : SELECT * FROM "users"     WHERE ("users"."id" = E'--- :user_id
> ')
>
> Any help is appreciated.
>
The first parameter to find (in the way you're trying to use it) is
the id of the record to load, but you're passing the symbol :user_id
(a symbol is an immutable interned string) which obviously can't work.
I assumed you're stashing the logged in user somewhere (can't remember
what restful auth does, might be current_user) you need to stick that
in the find call.

Fred
Posted by Bill Walton (Guest)
on 19.08.2008 21:32
(Received via mailing list)
Hi Joe,

Joe Sutherland wrote:

> The login page redirects to the user page, on successful log in, whose
> code looks like this:
>
> def userpage
>     login_required
>     @user = User.find(:user_id)
> end

If user_id is the value being entered into your login form, then try
User.find(params[:user_id]).  If not, you'll need to post some 
additional
code so we can see the flow you're trying to describe.  It's hard for me 
to
tell from your description exactly what's going on.

In Rails, pages don't direct or redirect to other pages.  Pages send
requests to methods (e.g. def update) in Controllers which typically 
update
and/or retrieve data from a data store through a Model (e.g.
User.find(params[:user_id]) and then render a response via a View (e.g.,
userpage.html.erb).  That response typically takes the form, from a user
perspective, of a new page being displayed, but there're important steps 
in
between the request and response that we'd need to know about to help 
you
debug your problem.

HTHj,
Bill
Posted by Joe Sutherland (joeturtle)
on 20.08.2008 20:27
Dear Jodi,

Thanks very much for your help. I've got it working now.

Thanks to Frederick as well for explaining the cause behind my error. 
I've clearly got some more reading to do before I understand Ruby.