Search code examples
javascriptjqueryruby-on-railsajaxujs

Simulate Remote => True Link Click Using Javascript


I'm trying to simulate the response I get from clicking one of my Rails 3 AJAX links (remote => true) using javascript. But I wonder if I'm thinking about this whole thing the wrong way.

I have some AJAX links which swap page content in a dashboard. Like this:

<%= tabs_tag(:builder => RemoteTabsBuilder) do |tab| %>
          <%= tab.preview 'Preview', unit_surveys_preview_path(@unit), :class => "mylink", :remote => true %>
          <%= tab.sendpage 'Send', unit_surveys_send_path(@unit), :class => "mylink", :remote => true %>
          <%= tab.home 'Results', unit_responses_results_path(@unit), :class => "mylink", :remote => true %>
<% end %>

This works as intended, with the ajax:success event replacing the main section of the page content. But within one of the pages I have some javascript that creates a modal overlay and what I want to do is reload the page after it completes.

I tried re-loading the whole page using:

format.js { render :redirect}

In the controller but I don't want to reload the whole dashboard, I just want to reload the 'Results' page content. So I'm thinking I want to simulate clicking on my existing link:

<%= tab.home 'Results', unit_responses_results_path(@unit), :class => "mylink", :remote => true %>

But

1) How can I do that using javascript (or jQuery)? (Based on this thread: doing a remote => true call with javascript I believe I can use $.get to simulate the click but that the Rails UJS responses will not be triggered)

2) I feel like I'm over complicating this. Is there a better way?


Solution

  • 1) I don't think that reload of whole page is nice idea, but you can do this via

    window.location = 'http://stackoverflow.com/questions/tagged/ruby-on-rails'

    in response, but I think this is not, what are you looking for.

    2) I think best solution is to render unit response results response. You can do this:

    format.js do
      #set variables(Ex.: @unit)
      render 'unit_responses_results/index'
    end
    

    3) Or you can just add some script to remove overlay in your response.

    4) Also you can use $.get to simulate and then evaluate js that comes from server.