views:

322

answers:

2

I have a remote_form which works 100%

When a user clicks submit, it goes out grabs so data from the db or data scraped from another website, and updates the page seamlessly...

The problem I'm having now, is that I'd like to do the same thing, but do it without having the user click the submit button with an onload event. I think I'm going about it the wrong way:

Technically this works... it does get the right data, but instead of doing exactly what the remote_form_tag does it returns the data raw. No RJS replace_html.

Is there a way to skip the form_for altogether and just execute the controller action directly when the page loads?

<script type="text/javascript">
    $(window).load(function() {
        // executes when HTML-Document is loaded and DOM is ready
        // jQuery.facebox('Big loading! <img alt=\"Loading\" src=\"/images/loaders/loading.gif\" />');
        //$("#update_form").submit();
        $("#update_form").submit(function () { return false; });

    });
</script>

The form for

#html.erb
<%  form_remote_tag :url => { :action => "update_availables", :id => params[:id] },
    :loading => "jQuery.facebox('Big loading! <img alt=\"Loading\" src=\"/images/loaders/loading.gif\" />');",
    :html => { :method => "get" },
    :method => "get",
    :html => { :id => 'update_form' } do %>

The controller

  def update_availables
    @do_it = Available.get_new_availables :id => params[:id], :date => user_cart.getDate.to_s
    get_availables

    respond_to do |format|
      format.html
      format.js
    end
  end

RJS

page.replace_html :rooms, :partial => "available"
page << "jQuery.facebox.close();"
+2  A: 

Well, to get the same functionality as the user clicking the form you could use the javascript click method:

$(window).load(function() {

    $("#update_form input[type='submit']").click();

});

But, if you're submitting without any input from the user, why put the data in a form at all, why not just put an ajax call in the load handler which calls the action and updates the code directly?

$(window).load(function() {

    $.ajax({
      type: "GET",
      url: "scraper_url",
      data: {field1:val,field2:val},
      success: function(responseText){
        // update page
      }
    });

});
floyd
+1  A: 

I agree with floyd here. The way I usually approach this type of thing is to return the necessary JSON data from the Rails actions rather than using RJS to update the page. This has the benefits that JSON is usually very fast to generate, and it can easily be moved to the model as a to_json method and reused in many different contexts.

If updating the page is simple there's no reason you can't do it inline like this, but if it's more complex you can define it as a function in a static javascript file. The nice thing about that approach is that it keeps your javascript and ruby code more modular and decoupled. It's easier to debug and unit test because the javascript code stands on its own rather than being leakily "abstracted" (more like obfuscated) by the limited RJS methods. You also gain some performance benefits from static javascript file caching as well.

If you look around the web you'll notice there's not much mention of RJS recently in Rails blogs. That's because RJS was a hot idea back in 2006, but has fallen into disuse as javascript best practices have evolved. To understand this, you need to look at the history of AJAX. Rails was one of the first frameworks to have built-in AJAX support in 2005, and it did so using Prototype which was really the first modern Javascript framework but was still quite rough around the edges. At the time a very large number of web developers were looking for any way to avoid Javascript, which had an undeservedly terrible reputation due to DOM incompatibilities. Therefore people were a little overeager to paper over everything with Rails helpers. This was great when people were wowed with very simple AJAX, but as Javascript frameworks matured and people became more ambitious with their Javascript functionality it became clear that Javascript deserves to be a first-class citizen. Nowadays jQuery makes common javascript tasks so concise and elegant that the early Rails helpers appear clumsy even when their default functionality works. There are still probably some good use cases for RJS, but I haven't used it for years and I haven't missed it at all.

My advice is to embrace Javascript. Pick up Javascript: The Good Parts by Douglas Crockford, and then spend a lot of time with jQuery. You will be well-rewarded.

dasil003
Totally agree on the fading use of RJS. It was a clever idea, but I never liked it in practice. Of course, now it's hard to get developers to make all their custom JS unobtrusive...
floyd