Find(:user_id)

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!

I'm guessing you want...

User.find(params[:user_id])

or maybe

User.find(params[:id])

Hey Joe -

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!

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

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.

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

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

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.