Search code examples
ruby-on-rails-3urlroutespermalinks

Routing - why permalink "john.green" doesn't work?


In routes.rb I have set up

match "member/:permalink" => "accounts#profile"

when the user's url name is john-green, john_green, johngreen, everything is working fine, but when the url name is john.green, so as the permalink is taken just the first part - john Specifically, from the Rails debug window:

Request

Parameters:

{"permalink"=>"john",
 "format"=>"green"}

Why is not accepted the . (dot) in the permalink? Exist any feature for using that? EDIT when I use

  match "member/:permalink" => "accounts#profile", 
        :requirements       => { :permalink => /.+/ }

I get

{"requirements"=>{"permalink"=>/.+/},
 "permalink"=>"john",
 "format"=>"green"}

Solution

  • By default Rails treats everything after the dot as a format and looks like there's no easy way of overriding this behavior. But there's a bit dirty workaround that uses :requirements option:

    match 'member/:permalink' => 'accounts#profile', 
          :constraints        => { :permalink => /.+/ }
    

    By adding greedy regexp for the :permalink parameter you make Rails pass everything it finds after the member/ substring in the path into params[:permalink].