Map dashboard URLs in Rails
When a user logs in into a website and it has a specific page (e.g. a dashboard for his stuff), how should it be mapped?
There are websites which have example.com/user/
, and others that only have example.com/
, others examples.com/user/id
.
What's the best way to do that? How can you know which user is it if you开发者_运维百科 don't pass the :id in the URL?
In terms of the address you use for the dashboard - it doesn't really matter. The important thing is that you choose something that is consistent and logical in the context of your site.
For example, if your site is more of an app - a la basecamp - and the dashboard is the main page from which a user starts interacting with the app then you probably want something along the lines of example.com/, or example.com/home or even example.com/dashboard
If, on the other hand, you are building something more along the lines of an e-commerce site and by a dashboard page you mean a screen with links to order history, profile details etc, you might want something more like xxx.com/myaccount
Like i say, the important thing is that it makes sense in the context of the rest of your site
In terms of knowing which user is which, regardless of wether the id is in the url, the details depend on how you handle authentication in your app. If you don't have anything already set up, I'd strongly recommend taking a look at Devise. It will do pretty much everything you need to get authentication up and running quickly and easily in rails 3. It also works well with CanCan which is good for determining who can do what within your app
It depends on whether the dashboard is a singleton resource or not. I would probably create it as a singleton resource eg
config/routes.rb
match '/dashboard' => 'users#dashboard', :as => 'dashboard'
rake routes
dashboard /dashboard(.:format) {:action=>"dashboard", :controller=>"users"}
In the controllers, I have a #current_user function that returns the currently logged-in user based on the session information. That way, the dashboard returns only information for the current_user, but in my example the user can never access the dashboard of another user, which is typically the case (though some admin features call for such an ability.)
i think the RESTful way of that would be .com/users/:id/dashboard
if you dont use resources to route your app then you can use what you like the most.
精彩评论