开发者

Rails REST routing: dots in the resource item ID

开发者 https://www.devze.com 2023-02-15 03:17 出处:网络
I have following in my routes.rb: resources :users, :except => [:new, :create] do get \'friends\', :as => :friends, :on => :member, :to => \"users#friends\"

I have following in my routes.rb:

resources :users, :except => [:new, :create] do
    get 'friends', :as => :friends, :on => :member, :to => "users#friends"
end

and following in my user.rb:

def to_param
  self.login
end

And when, for example, user with dots in login (for example 'any.thing') comes from facebook, rails gives routing error (no route found, I suppose that's because it recognises anything after dot as a format or because of route constr开发者_如何转开发aints). How can I come over this error?


The following constrain definition permit the dot in id as well as any character except slash.

Supported formats must be explicitly defined (here .html and .json) to not to be taken by id.

resources :foobars,
  :constraints => { :id => /[^\/]+(?=\.html\z|\.json\z)|[^\/]+/ }

That constrain definition is worked with Rails 3.1

For earlier Rails versions you may need to backport look-ahead support in regin gem (it is vendored in rack-mount gem)


You could replace periods with another character:

def to_param
  login.gsub(/\./,"-") # note: 'self' is not needed here
end

user = User.find_by_login("bart.simpson")
user_path(user) # => "/users/bart-simpson"

EDIT

You're right, this fails to deal with unique logins that map to the same value. Maybe a better way is to use segment constraints in the route:

  match 'users/(:id)' => 'users#show', 
    :constraints => { :id => /[0-9A-Za-z\-\.]+/ }

This should allow "/users/bart-simpson" and /users/bart.simpson" to generate :id => "bart-simpson" and :id => "bart.simpson" respectively. You'd have to alter the regex to add all the acceptable characters for the URL.

Note that this is mentioned in the Rails Routing Guide, section 3.2:

By default dynamic segments don’t accept dots – this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment add a constraint which overrides this – for example :id => /[^\/]+/ allows anything except a slash.


To allow the :id segment to contain any character except '/':

match 'users/(:id)' => 'users#show', :constraints => {:id => /[^\/]+/}

It's written elsewhere in one of the answers, but this is IMO the simplest way.

0

精彩评论

暂无评论...
验证码 换一张
取 消