4

I've made a Rails app using UJS and remote=true links.

For a controller#index action, instead of showing the content on page load, I'd like to call the #index action via Javascript once the page has loaded.

In short, I want to trigger the same functionality that is carried out when clicking a link like this:

<%= link_to "My Link", index_controller_action, :remote => true %>

But I want this to happen on 'page ready' rather than having to click the link.

Should I use the JQuery $ajax function for this? I have hacked it for now by triggering a false 'click' action on the remote link, but I'm sure this isn't best practice...

  • Why refactor the code if it works right now? And all i can think of is to bind the 'falsy' click event on page load (the same thing you did). – MurifoX Jul 30 '13 at 13:16
  • Using the JQuery ajax function would be absolutely fine, yes. [Good example of such here](http://stackoverflow.com/a/5795326/385532), just trigger your function on page `ready`. – Matt Jul 30 '13 at 13:26
  • Using Jquery AJAX wouldnt give me access to the .js.erb templates that Im rendering after the controller responds. I would have to handle the response entirely in JS (without access to ERB helpers) which will make things a lot more challenging... – George Coltart Jul 30 '13 at 16:50
  • I would also want an answer to this question, I am in exactly the same situation. – Alexander Popov Mar 13 '15 at 06:39

0 Answers0