开发者

Mapping domain.com/user/1 to domain.com/loginName

Has anyone tried before using subdomain-fu to change domain.com/user/1 to domain.com/UserName ?

Are there any other plugins to make thi开发者_如何学Gos happen on rails 3?


You can do this by "overwriting" the to_param method in your User model.

Assuming you are using a User model, as you stated put some code like this in it:

  #cleans up the url so it's purdy.
  def to_param
    loginName # you probably want login_name or just name, but whatever it's your code
  end

So notice that you now have to change your User#show controller call though, since you really are doing a

User.find_by_name(params[:id] 

Lastly, we should be able to change the routes so that the last route in your routes.rb file says:

match ':id' => 'users#show'


What you want is called pretty URLs. Take a look here: Pretty (dated) RESTful URLs in Rails

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜