开发者

Devise: Users have unique urls, how do I prevent them from using controller action routes?

In my config file I have this line (note: I am using cached_slugs from the slugged gem):

match '/:id', :to => 'users#show', :as => 'user'

How do I prevent users from signing up with routes that are currently being used for controller actions?

For example, a user could sign up with the username 'users' and their profile's unique URL would be http://localhost:3000/users; however, I am using that route for the users#index action. I could always set it so users have to use the traditional way of http://localhost:3000/users/theusernametheychose but I would prefer it the other way for user friend开发者_运维技巧liness sake. Any suggestions on the best way to solve this? Thank you very much!


You could add a validation to your user model that checks agains the existing routes defined in the application:

class User < ActiveRecord::Base
  ...

  validates_exclusion_of :name, 
                         :in => Rails.application.routes.routes.map {|r| r.path.match(/\/(\w+)\//) }.compact.map{|m| m[1] }.uniq, 
                         :message => "Username %{value} is reserved.

end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜