Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I cache a partial retrieved through link_to_remote in rails?

I use link_to_remote to pull the dynamic contents of a partial onto the page. It's basically a long row from a database of "todo's" for the day.

So as the person goes through and checks them off, I want to show the updated list with a line through those that are finished.

Currently, I end up needing to click on the link_to_remote again after an item is ticked off, but would like it to redirect back to the "cached" page of to-do's but with the one item lined through.

How do I do that?

Here is the main view:

<% @campaigns.each do |campaign| %>
<!--link_to_remote(name, options = {}, html_options = nil)-->

<tr><td>   <%= link_to_remote(campaign.name, :update => "campaign_todo",
        :url => {:action => "campaign_todo",
                 :id => campaign.id 
          }
       ) %>  </td></tr> 


<% end %>
</table>

<div id="campaign_todo">


</div>

I'd like when the New/Create actions are done to go back to the page that redirected it there.

When someone wants to "do" a task, it takes them to the new action. here is the controller:

  def create
    @contact_call = ContactCall.new(params[:contact_call])


    if @contact_call.save
      flash[:notice] = "Successfully created contact call."
      redirect_to contact_path(@contact_call.contact_id)
    else
      render :action => 'new'
    end
  end

I switched to redirect_to :back, which takes me back to the main view shown above...but WITHOUT the PARTIAL. It means I need to reload the partial all over again, which is a time-consuming database call.

1) Is it possible to go back to a view that has a partial called through AJAX and still have those results show up? 2) Can that result, however, be marked via CSS to indicate that it has been "checked off"?

like image 985
Satchel Avatar asked Nov 15 '22 10:11

Satchel


1 Answers

I would render the to-do list item response from javascript ERB files. So when you make the link_to_remote call, instead of redirecting back to the page, instead render javascript.

You'd have the form in /app/views/contact_call/_form.html.erb

/app/views/contact_call/create.js.rjs

page.replace :contact_form, :partial => "contact_call/form", :locals => { :user => @user }
page.visual_effect :highlight, :contact_form

Your controller would then render the javascript, which would in turn replace html on your page with the latest version (and highlight it). And your page would load the partial with strike-through on completed items.

like image 108
Jesse Wolgamott Avatar answered Dec 09 '22 18:12

Jesse Wolgamott