redirect in router with block is not working with "do...end" blocks #8160

Closed
gravis opened this Issue Nov 9, 2012 · 5 comments

Projects

None yet

2 participants

gravis commented Nov 9, 2012

The code in the documentation (http://rubydoc.info/docs/rails/3.2.8/ActionDispatch/Routing/Redirection):

match 'jokes/:number', :to => redirect do |params, request|
  path = (params[:number].to_i.even? ? "/wheres-the-beef" : "/i-love-lamp")
  "http://#{request.host_with_port}/#{path}"
end

will raise a redirection argument not supported (ArgumentError) exception with rails 3.2.8 and ruby 1.9.3-p286 (not tested with other versions yet).

The exception looks like (when trying to start to app):

/var/www/myapp/staging/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/redirection.rb:120:in `redirect': redirection argument not supported (ArgumentError)
        from /var/www/myapp/staging/releases/20121109152239/config/routes.rb:12:in `block (2 levels) in <top (required)>'
        from /var/www/myapp/staging/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/mapper.rb:624:in `scope'
        from /var/www/myapp/staging/releases/20121109152239/config/routes.rb:3:in `block in <top (required)>'            from /var/www/myapp/staging/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:282:in `instance_exec'
        from /var/www/myapp/staging/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:282:in `eval_block'            from /var/www/myapp/staging/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:260:in `draw'            from /var/www/myapp/staging/releases/20121109152239/config/routes.rb:1:in `<top (required)>'

Changing the code to something like:

match 'jokes/:number', :to => redirect { |params, request|
  path = (params[:number].to_i.even? ? "/wheres-the-beef" : "/i-love-lamp")
  "http://#{request.host_with_port}/#{path}"
}

solved the issue.

Sorry, this is how Ruby works. do..end will be passed as argument to match in this case, not redirect, whereas the {} version will be passed to redirect instead.

If you need to use do..end, you will need to wrap match with parentheses, kinda like this:

match('jokes/:number', :to => redirect do |params, request|
  path = (params[:number].to_i.even? ? "/wheres-the-beef" : "/i-love-lamp")
  "http://#{request.host_with_port}/#{path}"
end)

Thanks!

gravis commented Nov 9, 2012

@carlosantoniodasilva what about a doc update then?

Well, yeah, sounds good :). It's fixed in master already, but not in 3-2-stable as you saw. Thanks.

gravis commented Nov 9, 2012

Awesome 👍
Thanks a lot ;)

@carlosantoniodasilva carlosantoniodasilva added a commit that referenced this issue Nov 9, 2012
@carlosantoniodasilva carlosantoniodasilva Fix redirect example block
See #8160. [ci skip]
a99d542

Done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment