开发者

How to make Ruby on Rails RESTful routes pretty and human friendly?

With Ruby on Rails, REST is a great concept in terms of simplifying things for the developer and making resources easy for machines to access, but it also produces phenomenally ugly URLs for humans.

For example, using the popular RESTful Authentication plugin to handle user login and authentication, it creates two controllers for authentication and authorization, users and sessions. This is because user represents the long term resources associated with a user, such as the login and password, while a session represents the login info associated with a login session such as the cookie resources. Thus, the login url for a site would be site.com/sessions/new.

By default, it also creates a route to help this: map.login '/login', :controller => 'sessions', :action => 'new'

However, this is really only a kludge, because on submit in order for the routes to work right, the form it creates uses <% form_tag session_path do -%>. This causes the post of the form to go to SessionsController#create, but if the form submission fails (such as due to bad credentials) the user's browser is left at site.com/session, which looks really awkward. Worse yet, if the user manually were to enter that url, it is unavailable unless I define an index method. If I change the form_tag to read <% form_tag login_path do -%> then this results in POSTS to SessionsController#new, which completely messes 开发者_运维知识库up the REST.

How can I have consistent and reasonable looking URLs with REST? Overall, I really am tempted by the convenience of using REST, but it seems pretty wrong that this should produce (negative) changes visible to the user.


I think on the whole REST provides consistent, intuitive and user-friendly URLs, particularly when it comes to retrieving resources (GET) which is usually what a user wants to do (the pages they most often bookmark). Usually, you are dealing with URLs like

/posts/34
/articles/456/edit
/companies

You've hit on an isolated (although unavoidable, and therefore very annoying) exception, to which I have never seen a really satisfying solution, other than using AJAX. The problem is that RESTful routes shine in applications that have carefully thought out domains, where the resources on the backend make sense even to the user (like the examples above) whereas session management is inherently obscure to the user.

If you're that unhappy with the /sessions URL, why not alias /login/submit to /sessions/create?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜