You have to use fragment caching

http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html

and write a private method like

def sample_from_cache
  @cache = read_fragment(:page => params[:page], :per_page => @per_page)
  return @cache unless @cache.blank?
  @cache = render_to_string(:partial => "search.html.erb", :locals => {
:sample1 => Sample1.paginate(:page => params[:page], :per_page =>
@per_page) } )
  write_fragment({:page => params[:page], :per_page => @per_page}, @cache)
end

and your action like this

def sample1
    @sample1 = sample_from_cache
    respond_to do |format|
      format.js
   end
end

sample.js.erb


jQuery('#show1').html('<%= escape_javascript ( @sample1 ) %>');




On Mon, Dec 1, 2014 at 6:24 PM, Logesh m <[email protected]> wrote:

> I have a view where I have multiple things to load and I make an ajax call
> from the index view to add the content on success of each call and I would
> like to cache each of it so as to make the performance better.
>
> *index.html.erb*
> <div id="show1">
>
>
>
> </div>
>
> <script>
>
> $.ajax({
>
>     type : "GET",
>
>     url : 'sample1',
>
>     error: function( xhr, tStatus, err ) {
>
>         $("#show1").html("Error");
>
>     }
>
> });
>
> </script>
>
>
> *test_controller.rb*
> def sample1
>
>
>
>     @sample1 = Sample1.paginate(:page => params[:page], :per_page =>
> @per_page)
>
>
>
>
>     respond_to do |format|
>
>       format.js
>
>     end
>
>   end
>
>
> *sample1.js.erb*
>
> jQuery('#show1').html('<%= escape_javascript render "sample1" %>');
>
> *_sample1.html.erb*
> <% @sample1.each do |value| %>
>
>
>
>   <div class="name">
>
>     <%= value.name %>
>
>   </div>
>
> <% end %>
>
>
> I have thousands of name and these values do not change often and I show
> 100 entries on each page. So, I wanted to cache these and display instead
> of rendering each time.
>
> Thanks in advance!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/0f10881c-5e35-4169-aa8e-b4f825a4d07b%40googlegroups.com
> <https://groups.google.com/d/msgid/rubyonrails-talk/0f10881c-5e35-4169-aa8e-b4f825a4d07b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAFKVRj_LKmw_LdfVRa6kGsotEn6ydRNjXD4KmbVJ1_t%3D0GMSWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to