Render partial using ujs and without AJAX call on rails 3?

You might want to take a look my project github.com/adamaig/complex-form-examples which is derived from some other people's work. The same methods work for rails 3 (i just upgraded the project locally for a test). The big issue is the deeply nested elements, but this project should demonstrate a ujs way of working with jquery and rails generated templates.

Up vote 0 down vote favorite 2 share g+ share fb share tw.

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 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 ;) javascript ruby-on-rails jquery-ajax ruby-on-rails-3 unobtrusive-javascript link|improve this question asked Jun 18 '10 at 17:47Andrea Pavoni1,833416.

EDIT: according to this: github.com/rails/prototype_legacy_helper seems that link_to_function is back in rails. Don't know if it's temporary or permanent :| – Andrea Pavoni Jun 19 '10 at 14:03.

Great link! Thank you, I will look into that code ;-) – Andrea Pavoni Dec 20 '10 at 9:01.

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(' f)) %>);" (note the change of interpolating the ruby code into a string for the erb tags) Now you should be able to do: "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 ;.

Thank you for your reply. My problem is that I won't 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 :P btw I'll try something different, and in case I'll paste here ;) – Andrea Pavoni Jun 19 '10 at 8:57.

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 :).

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 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.

– Andrea Pavoni Jun 19 '10 at 13:38.

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 :).

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 ;) – Andrea Pavoni Jun 21 '10 at 7:52.

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.

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.

– Andrea Pavoni Sep 16 '10 at 11:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions