0

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?

1 Answer 1

2

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);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Why do you say 0 performance hit? The div will contain uploadify which is 2 js files and a SWF, along with images?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.