getting a 'name' based URL in RESTful routes instead of an id based url

99封情书 提交于 2019-12-03 21:47:06

In your Gallery model, add a #to_param method that returns what you want in the URL (in this case, name). In your controller, you still access the value through params[:id] and you'll probably have to use Gallery#find_by_name instead of Gallery#find.

As long as you use the provided helpers (gallery_path, etc.), your URLs should be pretty.

Then you have to think of what happens if the gallery name is something not very URL-friendly like 'Autumn/Fall2009#1' where you have a forward slash (will change the routing) and a dash (will not be submitted to the server if left unencoded). So, you will need urlencoding when generating such URLs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!