Calling methods from one object in another?

I realize the title probably doesn’t make much sense, but that was the
best short description I could come up with.

Anyway, I have users and pages in my site, and was using the following
code in the users controller, which works fine:

def show @user = User.find(params[:id]) @title = @user.Fname + " " + @user.Lname en

However, if I go to the pages controller, and use the following code, I
get “Couldn’t find user without an id” for an error.

def home @title = "Home" @user = User.find(params[:id]) end

Using User.find(1) works just fine instead of User.find(params[:id])

I feel like I’m probably just restricting something to Users only
somehow, but can’t figure it out. Seems like it would be obvious to
someone more experienced, (I hope?)

On Apr 17, 6:26am, Nathan D. [email protected] wrote:

en

Using User.find(1) works just fine instead of User.find(params[:id])

I feel like I’m probably just restricting something to Users only
somehow, but can’t figure it out. Seems like it would be obvious to
someone more experienced, (I hope?)

Your show action will usually be supplied with a :id parameter (since
show usually means show me this particular user) via the url (eg /
users/123) or query parameters.
On the other hand it sounds like when you link to your home action in
the pages controller you’re not supplying an id parameter, and so
params[:id] is nil, so User.find(params[:id]) can’t work.
Which user did you want to show?

Fred

I wanted to pass it the id of the logged in user. I have some things
that I want to show up for some users and not for others. I wanted to
use @user.admin? for this, which works fine on the users pages.
however, since I can’t get the @user assignment to work for pages this
isn’t working either :frowning:

Aha! I’ve figured it out.

In case anyone else is wondering, I have a current_user defined in
sessions helper, so, I had to include sessionshelper, and set @user =
current_user!

Phil

On Sun, Apr 17, 2011 at 8:47 PM, Nathan D. [email protected]
wrote:

Aha! I’ve figured it out.

In case anyone else is wondering, I have a current_user defined in
sessions helper, so, I had to include sessionshelper, and set @user =
current_user!

Just in case it helps; if you have a current_user helper, you don’t need
to
assign anything to @user in a controller at all. You can just use
current_user in your view. :slight_smile: