Problems with redirect_to

Hello guys,

I have the following spec:

describe 'With successful save' do

  it 'Should redirect to the new page' do
    do_post
    response.should be_redirect
    response.should redirect_to( :action => 'show', :id =>

@static_page.permalink ) ## this is the line 118
end

end

And when I try to run it it gives me the following error:

ActionController::MethodNotAllowed in ‘StaticPagesController Handling
POST /pages With successful save Should redirect to the new page’
Only get, put, and delete requests are allowed.
./spec/controllers/static_pages_controller_spec.rb:118:
script/spec:4:

So, the response is a redirect (the “response.should be_redirect”
passes) but when i try to be sure that it redirected to the right page
it throws this weird error to me. Any ideas about what I’ve done
wrong?

I’m using RSpec 1.1.3 and Rails 2.0.2.


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 7:50 AM, Maurício Linhares
[email protected] wrote:

@static_page.permalink ) ## this is the line 118
./spec/controllers/static_pages_controller_spec.rb:118:
script/spec:4:

So, the response is a redirect (the “response.should be_redirect”
passes) but when i try to be sure that it redirected to the right page
it throws this weird error to me. Any ideas about what I’ve done
wrong?

Can you post the full backtrace?

script/spec ./spec/controllers/static_pages_controller_spec.rb -b

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.

And here goes the full backtrace:

ActionController::MethodNotAllowed in ‘StaticPagesController Handling
POST /pages With successful save Should redirect to the new page’
Only get, put, and delete requests are allowed.
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1439:in
recognize_path' /home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:43:inpath_hash’
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:38:in
hash_from_url' /home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:26:inactual_hash’
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:34:in
actual_redirect_to_valid_route' /home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:18:inmatches?’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/expectations/handler.rb:19:in
handle_matcher' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb:34:inshould’
./spec/controllers/static_pages_controller_spec.rb:119:
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:78:in
instance_eval' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:78:inrun_with_description_capturing’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:19:in
execute' /usr/lib/ruby/1.8/timeout.rb:48:intimeout’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:16:in
execute' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:292:inexecute_examples’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:291:in
each' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:291:inexecute_examples’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:121:in
run' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:22:inrun’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:in
each' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:inrun’
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/options.rb:90:in
run_examples' /home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/command_line.rb:19:inrun’
script/spec:4:

On Fri, Feb 22, 2008 at 11:00 AM, David C. [email protected]
wrote:

    response.should be_redirect

POST /pages With successful save Should redirect to the new page’

João Pessoa, PB, +55 83 8867-7208


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


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 8:10 AM, Maurício Linhares
[email protected] wrote:

@static_page.permalink ) } ## thats what i’m testing agains
And here is the spec:
@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

On Fri, Feb 22, 2008 at 9:20 AM, Maurício Linhares
[email protected] wrote:

“/pages/test” and sent it two the recognize_path method, but this path
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?

That would be great, thanks! Please do review
http://rspec.lighthouseapp.com/projects/5645/how-to-file-a-ticket
before you do.

Thank you,
David

Hi David,

First, using the controller doesn’t work also :slight_smile:

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

David C. wrote:

On Fri, Feb 22, 2008 at 9:20 AM, Maur�cio Linhares
[email protected] wrote:

“/pages/test” and sent it two the recognize_path method, but this path
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?

That would be great, thanks! Please do review
http://rspec.lighthouseapp.com/projects/5645/how-to-file-a-ticket
before you do.

This bug are a ticket or not ? Because I have the same problem and I
don’t find in lighthouse bug tracking.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs