问题
I'm refactoring a rails 3 application, and want to know how best to deal with ajax based sub components?
I have a 'dashboard' controller. Which is rendered via the 'dashboard#show' action.
And I now want to have an ajax based 'tabs' component, which is part of the dashboard.
I have two possible solutions:
solution 1: implement the tabs component as an action e.g. 'dashboard#tabs' (this is my current solution). This gives me ugly helpers:
- tabs_dashboard_path
solution 2: implented it as nested resource e.g. 'dashboard/tabs#show'. This will give nicely named helpers e.g.:
- dashboard_tabs_path
Baring in mind I have a lot of other ajax components what would be the best course of action?
If I was to used solution 2 my resources would become deeply nested, and they would only have one 'show' action; this would seem a bit too verbose for my liking.
For instance the routes would be specified as:
resource :dashboard, :only => [:show], :controller => "users/dashboard" do
resource :tabs :only => [:show], :controller => "users/dashboard/tabs" do
resource :steps :only => [:show], :controller => "users/dashboard/tabs/steps"
#etc....
end
end
Is there a good rails way for dealing with this?
回答1:
I tend to keep my controllers with very few actions, and one that only deals with the basic REST verbs. Therefore, in a multi-show situation, I'd keep the single dashboard#show
and have partials/layouts to handle the tabs. My rationale is that the tabs themselves are not resources, nor would I actually perform any RESTful actions on them.
来源:https://stackoverflow.com/questions/9519617/how-do-i-deal-with-ajax-based-sub-components-in-rails-3