Regex with a period route fails #3301

Closed
tc opened this Issue Oct 12, 2011 · 8 comments

Comments

Projects
None yet
6 participants
@tc

tc commented Oct 12, 2011

I have a route file where a regex matcher works in a resource scope but not in the match method:

routes.rb:

  regex = /[\w\d\s!\.]+?/
  resources :pages, :id => regex do
    member do
      get 'embed'
    end
  end
  match "/embed/:id" => "pages#embed", :id => regex

pages_controller.rb:

  def embed
    id = params[:id]
    render :text => "id is #{id}"
  end

This route works:
http://localhost:3000/pages/Edwin.Lee/embed
id is Edwin.Lee

This one fails:
http://localhost:3000/embed/Edwin.Lee
id is Edwin

@alindeman

This comment has been minimized.

Show comment Hide comment
@alindeman

alindeman Oct 12, 2011

Contributor

What happens if your regex is greedy? .. e.g., /[\w\d\s!\.]+/

Contributor

alindeman commented Oct 12, 2011

What happens if your regex is greedy? .. e.g., /[\w\d\s!\.]+/

@tc

This comment has been minimized.

Show comment Hide comment
@tc

tc Oct 12, 2011

using the greedy regex works as a workaround, but it's still odd there's a different behavior when using the same regex with resources/match.

tc commented Oct 12, 2011

using the greedy regex works as a workaround, but it's still odd there's a different behavior when using the same regex with resources/match.

@ghost ghost assigned tenderlove Oct 12, 2011

@alindeman

This comment has been minimized.

Show comment Hide comment
@alindeman

alindeman Oct 14, 2011

Contributor

I have yet to verify myself, but I'm betting this has something to do with Rails automatically expecting a .format at the end of routes. If your regex doesn't capture it, it'll be picked up as a .format.

To verify, check the value of params[:format] in your controller when the non-greedy regex is used.

Contributor

alindeman commented Oct 14, 2011

I have yet to verify myself, but I'm betting this has something to do with Rails automatically expecting a .format at the end of routes. If your regex doesn't capture it, it'll be picked up as a .format.

To verify, check the value of params[:format] in your controller when the non-greedy regex is used.

@tc

This comment has been minimized.

Show comment Hide comment
@tc

tc Oct 15, 2011

That makes sense, but what doesn't is the different behavior of match and resource methods. Shouldn't the params capturing behave the same way?

tc commented Oct 15, 2011

That makes sense, but what doesn't is the different behavior of match and resource methods. Shouldn't the params capturing behave the same way?

@alindeman

This comment has been minimized.

Show comment Hide comment
@alindeman

alindeman Oct 15, 2011

Contributor

.format is only at the very end.

Contributor

alindeman commented Oct 15, 2011

.format is only at the very end.

@isaacsanders

This comment has been minimized.

Show comment Hide comment
@isaacsanders

isaacsanders Apr 28, 2012

Contributor

@tc Is this still an issue?

Contributor

isaacsanders commented Apr 28, 2012

@tc Is this still an issue?

@tc

This comment has been minimized.

Show comment Hide comment
@tc

tc Apr 30, 2012

@alindeman suggested a workaround by using a greedy regex expression.

  regex = /[\w\d\s!\.]+?/
  greedy_regex = /[\w\d\s!\.]+/

Using the non-greedy regex on http://localhost:3000/embed/Edwin.Lee will capture Edwin(instead of Edwin.Lee) as params[:id]

This can be closed if this is the intended behavior.

tc commented Apr 30, 2012

@alindeman suggested a workaround by using a greedy regex expression.

  regex = /[\w\d\s!\.]+?/
  greedy_regex = /[\w\d\s!\.]+/

Using the non-greedy regex on http://localhost:3000/embed/Edwin.Lee will capture Edwin(instead of Edwin.Lee) as params[:id]

This can be closed if this is the intended behavior.

@kytrinyx

This comment has been minimized.

Show comment Hide comment
@kytrinyx

kytrinyx Jul 14, 2012

Contributor

@steveklabnik It looks like this issue can be closed.

Contributor

kytrinyx commented Jul 14, 2012

@steveklabnik It looks like this issue can be closed.

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