Reputation: 1455
I'm putting in place a web app with RoR and want to implement the history API from HTML5.
Let's say I have a controller 'Item', a view 'Item_view', and a div 'div' inside 'Item_view'.
When I dynamically load 'div' (on click for example), do I:
The first option consumes the same resources as loading the entire page, AJAX here is useless (right?). But the second option implies that I need a controller for each part of the page I want to dynamically load. Or is there another way of doing it? Any way of filtering what is loaded by the controller according to request type? Any best practice tips?
Thanks.
Upvotes: 1
Views: 592
Reputation: 17703
I would call the item controller and action that you would normally call (show, new, edit). Put the div code into a partial called _item_view.html.erb.
Using the respond_to block with an AJAX request, you can load just the partial that you need along with the objects the partial needs instead of loading the entire HTML block of the show/edit/new action. If you don't need to build any objects, just remove the @items instance variable and the :collection => @items assignment.
So if you were going to show a collection of items, the following would render the item_view partial for each item in the @items collection and return that the to JavaScript.
def index
@items = Item.all
respond_to do |format|
format.html {...code...}
format.js {
render :layout => false, :partial => 'item_view', :collection => @items
}
end
end
Upvotes: 1