Jay McGavren's Journal

2008-08-25

Learned the hard way...

When you’re setting up logins in Rails, the tutorials tell you to store action_name and controller_name in the session so you can redirect there later… Capture request.path instead. This works just fine:

redirect_to :controller => session['intended_controller'], :action => session['intended_action']

…until you’re using restful resources and a logged-out user clicks an edit link. When Rails tries to take them to “/entries/edit” (controller followed by action) instead of “/entries/1/edit”, it’ll bomb:

ActiveRecord::RecordNotFound in EntriesController#show

Couldn't find Entry with ID=edit

So this is better:

redirect_to session['intended_path'] #request.path stored earlier

Oh, and when they say to put this in your ApplicationController:

before_filter :authenticate, :authorize, :except => [:login]

Do this instead:

before_filter :authenticate, :authorize

…and then put this in specific Controllers where you need the exception:

skip_before_filter :authenticate, :authorize, :only => [:login, :verify]
comments powered by Disqus