Jason
Jason

Reputation: 1784

New Action Not Working

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?

Upvotes: 2

Views: 489

Answers (5)

Srinivas M.V.
Srinivas M.V.

Reputation: 6608

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 !

Upvotes: 0

Julian Browne
Julian Browne

Reputation: 91

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: Rails cannot find model with same name as Ruby class

Long shot maybe.

Upvotes: 0

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11811

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

Upvotes: 0

Preston Marshall
Preston Marshall

Reputation: 1195

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.

Upvotes: 1

jdl
jdl

Reputation: 17790

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?

Upvotes: 0

Related Questions