1

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?

Community
  • 1
  • 1
andyleesuk
  • 110
  • 10

1 Answers1

0

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.

Zh Kostev
  • 588
  • 3
  • 17
  • ... re: 1) reloading the page is not ideal. 2) I don't think I want to render, but rather redirect as I don't want to repeat everything that's already in the responses#results controller. However if I redirect then I have no way of accessing that redirected output as it is not bound to rails UJS Ajax events(?!). 3) I can remove the overlay but I want to rerun the responses#results controller (because it does a lot of complex stuff). 4) this is maybe what I need to do, but seems a bit of a hack and a rework of functionality that exists elsewhere (i.e. in the data-remote link) – andyleesuk Jul 24 '14 at 14:41
  • Thanks again. I have gone with option 4) using $.get linked to unit_responses_results_path(@unit) and then re-running the other javascript I have in the UJS success event. I will probably wrap them into a function to make it a bit DRYer but it still feels like a long way round recreating functionality that already existed. – andyleesuk Jul 24 '14 at 16:58