开发者

Rails URL question

If I have a Users mod开发者_开发百科el, and then an individual user, say, person1, is there a way to have the url: www.myapp.com/person1 show what www.myapp.com/users/person1 would? What would that be called?

Thank you!


You should define a route in your routes.rb file:

match "/:user" => "users#show"

and you'll get the username given in params[:user]. But you need to know that this kind of route could override other routes defined, because it will match any string, so you should at least define some constraints on the username.

For example if your usernames matches a regexp you could define it as a constraint

match "/:user" => "users#show", :constraints => { :user => /some-regexp/ }

and don't forget to set this route as the last one in your routes file, otherwise it will clash for sure with other routes.

Read this for full reference


add "two" routes for the same thing to your app.

 # Shorter url to show store items by tags
map.connect '/s/tags',
  :controller => 'music',
  :action     => 'show_by_tags'
map.connect '/music/s/tags',
  :controller => 'music',
  :action     => 'show_by_tags'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜