开发者

Rails - Using jQuery to populate a form

开发者 https://www.devze.com 2023-01-29 02:07 出处:网络
on my page I have the following: <span id=\"attach-file\" class=\"link\">Attach a file</span>

on my page I have the following:

<span id="attach-file" class="link">Attach a file</span>
  <div id="attach-file-form">
</div>

Give that attaching a file is not a common use case, I don't want the attach-file-form elements to be present on load, it would slow everything 开发者_如何学编程down.

What I would like to happen is the user clicks "Attach a file", jQuery AJAX GET to get the form and inject it inside of attach-file-form.

What's the right way in Rails to go about this?

in jQuery I have:

$("#attach-file").live("click", function() {
    DO A GET TO A custom Method in the Attachment Controller
    Inject inside the div
});

Does this sound right?


Having the file upload form present on the page but hidden will have pretty much zero impact on the performance of your site. I'd recommend just defaulting the file upload form to hidden, and triggering display of the form when your button is clicked.

Then your JQuery code can be as simple as:

$("#attach-file").live("click", function() {
  $("#file_upload_form").show();
});

If you do need to get this from the server, you can use the jQuery.get method to make a call to a Rails controller, which can output the form for you:

$("#attach-file").live("click", function() {
  $.get("/controller/action", function(html) {
    $("#file_upload_form").html(html);
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消