views:

626

answers:

4

I need to do multiple file uploads using nested form and jQuery. so I wrote an helper using link_to_function method:

  def add_document_link(title, form)
    link_to_function title do |page|
      form.fields_for :documents, Document.new, :child_index => Time.now.to_i do |f|
        page << "$('#documents').append('#{escape_javascript(render('/realties/document', :f => f))}');"
      end
    end
  end

this code simply creates a link that, if pressed, renders a new file upload nested form. it surprisingly works, even if I'm using rails3 beta4 (and I know that 'link_to_function' is deprecated)

is there a better way to do the same without using link_to_function or some AJAX call?

thanks in advance ;)

+1  A: 

If it works then the only thing left to do is to make it look prettier by moving the JavaScript call to a separate whatever.js.erb file:

whatever.js.erb:

"$('#documents').append('<%= escape_javascript(render('/realties/document', :f => f)) %>);"

(note the change of interpolating the ruby code into a string for the erb tags)

Now you should be able to do:

<%= link_to title, { :action => "whatever" }, :remote => true %>

The code will be much more concise and the outputed html will look way better, because now there isn't any javascript in there. Just make sure that your document is html5 and that you include the JS libraries (which you probably do since your code works :)).

I myself am using Prototype, but it shouldn't differ in JQuery. The only weak spot that I can think of now is passing the form block to the partial, but it should work and it's too late to think :)

Post here whether it worked or not and if not we'll figure it out. Good night now ;]

The Lord
thank you for your reply. my problem is that I wont use a remote action. the code I pasted works on client-side without the need to create a remote action.I'd like to do the same thing using something less-obtrusive :Pbtw I'll try something different, and in case I'll paste here ;)
apeacox
A: 

Ok sorry, but I didn't notice that you don't want ajax call. I am not so sure if calling the page object doesn't make that call anyway though. I think that to call it entirely without ajax you would have to cache the field some way on the client side. Are you totally sure that your example works without an ajax call?

If it does the only thing I can think of to make it "less obtrusive" is to extract the javascript to application.js file which is meant for storing your own javascript and just call the function from there inside your view. That is if you include that file (application.js). This will simply move the javascript away from the view without changing the way your code works at all.

Cheers :)

The Lord
thank you for your reply. I think it's not easy to cache that form,otherwise I could generate html with jquery.I also tryed to use some javascript outside tags but it doesn't work. but it doesn't work with ajax call too.that's because it needs FormBuilder instances...with rails < 3, using obtrusive JS, this never was a problem. I've always used my helpers (like the one posted) to render the nested forms, without any ajax call.now, with rails3, link_to_function is deprecated, ok. but I'd really like to know if there's another way to achieve the same task.
apeacox
A: 

This really exceedes my experience with nested forms and UJS, but here's another idea try not passing the FormBuilder instance into the partial. Maybe it'll just work. I've seen stranger things work. This may be right since you render it INSIDE that form so the instance might be reachable without passing it directly. If this doesn't work then it will take someone wiser than me to answer your question :)

The Lord
to get this work by not passing a FormBuilder instance, I should create an *ad hoc* html form on the fly, using proper attributes for tags. It'll work for sure, but it's not clean :/thank you for your replies ;)
apeacox
A: 

See RailsCasts episodes 196 and 197

I apologize of the link-only answer, but no one says it better than Ryan:

196. Nested Model Form Part 1

197. Nested Model Form Part 2

SooDesuNe
thank you for your reply, that's the approach I've followed in several projects (using jquery instead of prototype). btw it's not an unobtrusive solution. what I was looking for is something unobtrusive with the same result.
apeacox