[rspec-users] Problems with redirect_to

Maurício Linhares mauricio.linhares at gmail.com
Fri Feb 22 10:20:59 EST 2008


Hi David,

First, using the controller doesn't work also :)

After some debugging here i found out why it was trowing the error,
the RedirectTo matcher uses the
ActionController::Routing::Routes.recognize_path method to discover if
a path exists and this method takes two parameters, the path and the
"environment" that the path is being requested. When my code was being
matched, the redirect_to matcher generated the right path
"/pages/test" and sent it two the recognize_path method, but this path
is a Restful one and only GET, PUT and DELETEs are allowed and the
RedirectTo matcher didn't send any :method param, so when the path is
being recognized and no method is sent it will never be recognized and
thus that weird error is thrown.

A simple way to solve this issue is change the path_hash method at the
RedirectTo matcher to include the HTTP method being called:

        def path_hash(url)
          path = url.sub(%r{^\w+://#{@request.host}}, "").split("?", 2)[0]
          ActionController::Routing::Routes.recognize_path path, :method => :get
        end

As you can only redirect to GET requests, this should not be a
problem. Should i file a patch for this?

Now, the two methods are working fine =D

      it 'Should redirect to the new page' do
        do_post
        response.should redirect_to( page_path( :action => 'show', :id
=> @static_page.permalink ) )
        response.should redirect_to( :action => 'show', :id =>
@static_page.permalink )
      end


-- 
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) |
http://codeshooter.wordpress.com/ (en)
João Pessoa, PB, +55 83 8867-7208


On Fri, Feb 22, 2008 at 11:25 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
> On Fri, Feb 22, 2008 at 8:10 AM, Maurício Linhares
>
> <mauricio.linhares at gmail.com> wrote:
>
>
> > Using the "path" method the spec passes, but i don't use the path
>  >  method on my controller. First, here's my controller code:
>  >
>  >   def create
>  >     @static_page = @event.static_pages.build(params[:static_page])
>  >
>  >     respond_to do |format|
>  >       if @static_page.save
>  >         flash[:notice] = 'Page was successfully created.'
>  >         format.html { redirect_to(:action => 'show', :id =>
>  >  @static_page.permalink ) } ## thats what i'm testing agains
>  >         format.xml  { render :xml => @static_page, :status =>
>  >  :created, :location => @static_page }
>  >       else
>  >         format.html { render :action => "new" }
>  >         format.xml  { render :xml => @static_page.errors, :status =>
>  >  :unprocessable_entity }
>  >       end
>  >     end
>  >   end
>  >
>  >  And here is the spec:
>  >
>  >
>  >       it 'Should redirect to the new page' do
>  >         do_post
>  >         response.should be_redirect
>  >         response.should redirect_to( page_path( :action => 'show', :id
>  >  => @static_page.permalink ) ) ## this one, with the page_path goes
>  >  fine
>  >
>  >         response.should redirect_to( :action => 'show', :id =>
>  >  @static_page.permalink ) ## this one, without the page_path doesn't
>  >  work
>  >       end
>  >
>  >  It seems that the redirect_to matcher doesn't recognize the current controller.
>
>  Ah. I missed that the initial example was missing the controller
>  because I was focused on the error message, which is obviously a red
>  herring.
>
>  Try adding the controller to the hash:
>
>  esponse.should redirect_to( :controller => 'static_pages', :action =>
>
> 'show', :id => @static_page.permalink )
>
>  If that works, then please submit a bug report to the tracker (that
>  redirect_to does not work with action + id unless you specify the
>  controller).
>
>  Thanks,
>  David
>


More information about the rspec-users mailing list