views:

82

answers:

5

I have a standard User controller with the normal set of actions (index, show, new, edit, etc) and I'm trying to add a new action named 'profile'. I added the following code:

def profile
    @user = User.find(session[:user_id])
end

I also created a new view for the action (app/views/users/profile.html.erb), but whenever I try to view that page I get an error:

ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=profile
...

Apparently it's hitting the show action. I'm guessing that means I need to add something to my routes to make this work, but I don't know what. So far I just have the two default routes and the map.root line which I uncommented:

map.root :controller => "home"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

So really I have two questions:

  1. What do I have to do in order to enable my new action?
  2. Why don't the existing routes cover this situation? Other urls consisting of just the controller and action work just fine (e.g. http://localhost:3000/users/new). Why not this one? Shouldn't it just evaluate to :controller = users, :action = profile, :id = nil?
A: 

Are you intentionally trying to get the id from session[:user_id] instead of params[:id]? Is this supposed to be displaying a public profile?

jdl
On the profile page I'm getting the id from session (it's for the currently logged in user) while on the show page it's getting it from the params since show can be used for any user, not just whoever is logged in.
Jason
+1  A: 

Try putting something like this in your routes.rb file:

map.user_profile '/users/:id/profile', :controller => "users", :action => 'profile', :conditions => {:method => :get}

I think possibly the reason it's doing this is because you are not matching either of the defaults, because you are not setting :id (even though it is detecting your action as the id). I don't know what your URL looks like, but I have a feeling that if you tried http://localhost:3000/users/123124124/profile, it MIGHT work, even without the new line in routes.

Preston Marshall
A: 

Are you sure, that the session contains the user_id the first time you load the page?

Lichtamberg
A: 

Hard to say without seeing all the code, but my guess is there may be some strangeness because your model and controller have the same name. I'd try renaming the controller before changing anything else (remember to change the name of the views/users directory too).

See also this other stack overflow post: http://stackoverflow.com/questions/960781/rails-cannot-find-model-with-same-name-as-ruby-class

Long shot maybe.

Julian Browne
A: 

Solution for your problem configure your routes.rb in such a way that id should be passed as the parameter .

configure in routes.rb as below

 map.profile '/profile/:id',:controller=>'users',:action=>'profile'

when you want to access your profile page use it with the following URL

http://localhost:3000/profile

Make sure once you login , u handle the session and store the userid in the session variable .

Good luck !

YetAnotherCoder