Hey All, In my app, I have actions that are restricted to only POST requests in config/routes.rb. We were getting a fair number of MethodNotAllowed errors on these actions. In most cases, we''re ok with sending the user back to the homepage, however there are a few exceptions where we''d like to send them a specific action. My spec looks like this: describe "GET process_step_2" do it "should redirect to step_2" do rescue_action_in_public! get :process_step_2 response.should redirect_to(application_step_2_url) end end Then my application controller, I decided to rescue_from, so it looks like this: class ApplicationController < ActionController::Base rescue_from ActionController::MethodNotAllowed, :with => :invalid_method protected def invalid_method redirect_to previous_step_url end def previous_step_url case request.env["REQUEST_URI"] when /step_2\/process/ application_step_2_url else root_url end end end The spec fails even though when you re-create the situation in the browser, the code works. What am I doing wrong here? Am I even going about this in the right way? Thanks in advance for your help. Best, PJ
On Mon, Jun 22, 2009 at 5:19 PM, PJ Kelly<pj at crushlovely.com> wrote:> Hey All, > > In my app, I have actions that are restricted to only POST requests in > config/routes.rb. ?We were getting a fair number of MethodNotAllowed > errors on these actions. ?In most cases, we''re ok with sending the > user back to the homepage, however there are a few exceptions where > we''d like to send them a specific action. ?My spec looks like this: > > ?describe "GET process_step_2" do > ? ?it "should redirect to step_2" do > ? ? ?rescue_action_in_public! > ? ? ?get :process_step_2 > ? ? ?response.should redirect_to(application_step_2_url) > ? ?end > ?end > > Then my application controller, I decided to rescue_from, so it looks > like this: > > class ApplicationController < ActionController::Base > ?rescue_from ActionController::MethodNotAllowed, :with > => :invalid_method > > ?protected > > ?def invalid_method > ? ?redirect_to previous_step_url > ?end > > ?def previous_step_url > ? ?case request.env["REQUEST_URI"] > ? ?when /step_2\/process/ > ? ? ?application_step_2_url > ? ?else > ? ? ?root_url > ? ?end > ?end > end > > The spec fails even though when you re-create the situation in the > browser, the code works.What''s the failure message?> > What am I doing wrong here? ?Am I even going about this in the right > way? > > Thanks in advance for your help. > > Best, PJ > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Hi David, thanks for the speedy response yesterday. Much appreciated. On Jun 22, 3:43?pm, David Chelimsky <dchelim... at gmail.com> wrote:> What''s the failure message?''MembersController GET process_step_2 should redirect to step_2'' FAILED expected redirect to "http://test.host/application/step_2", got no redirect I did some more digging, and found a couple things. When I try this in the browser (by visiting /application/step_2/process) and look at the development log, the request is actually being made to ApplicationController#index: Processing ApplicationController#index (for 127.0.0.1 at 2009-06-23 11:08:18) [GET] Redirected to http://byassociation.local/application/step_2 However when the specs are run, the test log shows: Processing MembersController#process_step_2 (for 208.77.188.166 at 2009-06-23 11:19:50) [GET] I guess the question then, is that if this is how things are handled when using rescue_from, how do I spec out that users are being redirected to the right location? Do you think the inconsistency between the development test logs needs to be brought up with Rails core? Thanks in advance! Best, PJ
On Tue, Jun 23, 2009 at 1:27 PM, PJ Kelly<pj at crushlovely.com> wrote:> Hi David, thanks for the speedy response yesterday. ?Much appreciated. > > On Jun 22, 3:43?pm, David Chelimsky <dchelim... at gmail.com> wrote: >> What''s the failure message? > > ''MembersController GET process_step_2 should redirect to step_2'' > FAILED > expected redirect to "http://test.host/application/step_2", got no > redirect > > I did some more digging, and found a couple things. ?When I try this > in the browser (by visiting /application/step_2/process) and look at > the development log, the request is actually being made to > ApplicationController#index: > > Processing ApplicationController#index (for 127.0.0.1 at 2009-06-23 > 11:08:18) [GET] > Redirected to http://byassociation.local/application/step_2 > > However when the specs are run, the test log shows: > > Processing MembersController#process_step_2 (for 208.77.188.166 at > 2009-06-23 11:19:50) [GET] > > I guess the question then, is that if this is how things are handled > when using rescue_from, how do I spec out that users are being > redirected to the right location? ?Do you think the inconsistency > between the development test logs needs to be brought up with Rails > core?You''ve got a route named application :) I''m sure that''s where the confusion lies, though I''m not convinced of where the problem lies. What are the controllers and models actually called? What version of rails, rspec, etc?> > Thanks in advance! > > Best, PJ > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >