According to http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000366 "The result of that request can then be inserted into a DOM object whose id can be specified with options[:update]. Usually, the result would be a partial prepared by the controller with either render_partial or render_partial_collection." I''ve got a set of divs with ids generated based on data in the model. eg. c1, c2, c3, ... At the moment when something is added via form_remote_tag it is dispayed in a default new-items div, then the page has to be refreshed to get the new item sorted into the correct display div. Is there some rails way to chose which div to update based on the processing of the form by the controller? ie. is it possible to defer or rewrite :update after or during the controller''s (:url) run? NIcholas
Instead of specifying a :div with update, specify a javascript function with :complete :complete => "myfunction(request)" and then in javascript: function myfunction(request) { if(something) { new Insertion.Bottom ($(''div1''), request.responseText) } else { new Insertion.Bottom ($(''div2''), request.responseText) } } Something like that. Tyler On 6/19/05, Nicholas Lee <emptysands-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> According to http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000366 > > "The result of that request can then be inserted into a DOM object > whose id can be specified with options[:update]. Usually, the result > would be a partial prepared by the controller with either > render_partial or render_partial_collection." > > I''ve got a set of divs with ids generated based on data in the model. > eg. c1, c2, c3, ... At the moment when something is added via > form_remote_tag it is dispayed in a default new-items div, then the > page has to be refreshed to get the new item sorted into the correct > display div. > > Is there some rails way to chose which div to update based on the > processing of the form by the controller? > > ie. is it possible to defer or rewrite :update after or during the > controller''s (:url) run? > > > NIcholas > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
To have even more control, you can also generate JavaScript on the Server and have it executed with: :complete => "eval(request.responseText)" This way, the server action can completetly decide what should happen. There are some examples of this in my chapter in the BetaBook V2, on pages 391 to 392. Basically, you generate something like this on the server: new Insertion.Bottom(''<%= @which_div %>'', ''<%= escape_javascript(render(:partial => "my_partial")) %>''); Thomas Am 20.06.2005 um 01:52 schrieb Tyler Kiley:> Instead of specifying a :div with update, specify a javascript > function with :complete > > :complete => "myfunction(request)" > > and then in javascript: > > function myfunction(request) { > if(something) { > new Insertion.Bottom ($(''div1''), request.responseText) > } else { > new Insertion.Bottom ($(''div2''), request.responseText) > } > } > > Something like that. > > Tyler > > On 6/19/05, Nicholas Lee <emptysands-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> According to http://api.rubyonrails.com/classes/ActionView/Helpers/ >> JavascriptHelper.html#M000366 >> >> "The result of that request can then be inserted into a DOM object >> whose id can be specified with options[:update]. Usually, the result >> would be a partial prepared by the controller with either >> render_partial or render_partial_collection." >> >> I''ve got a set of divs with ids generated based on data in the model. >> eg. c1, c2, c3, ... At the moment when something is added via >> form_remote_tag it is dispayed in a default new-items div, then the >> page has to be refreshed to get the new item sorted into the correct >> display div. >> >> Is there some rails way to chose which div to update based on the >> processing of the form by the controller? >> >> ie. is it possible to defer or rewrite :update after or during the >> controller''s (:url) run? >> >> >> NIcholas >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 6/20/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote:> To have even more control, you can also generate JavaScript > on the Server and have it executed with: > > :complete => "eval(request.responseText)" > > This way, the server action can completetly decide what should > happen. > > There are some examples of this in my chapter in the BetaBook V2, > on pages 391 to 392.Excellent. Missed that bit in the book. Still trying to suck it all in. ;)> Basically, you generate something like this on the server: > > new Insertion.Bottom(''<%= @which_div %>'', > ''<%= escape_javascript(render(:partial => "my_partial")) %>'');That''s pretty good. Means you can use ruby instead of javascript for app display logic in the controller. Nicholas
>> "eval(request.responseText)"Isnt this dangerous ? I mean, doesn''t it open your server to executing any old ''responseText'', not just the "approved" version pushed to the browser? Nev
On 6/21/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote:> >> "eval(request.responseText)" > > Isnt this dangerous ? > > I mean, doesn''t it open your server to executing any old ''responseText'', > not just the "approved" version pushed to the browser?You mean the client. The eval() runs client side, via javascript in the browser. At the moment, I''m trying to figure out this method why plain text works, but a complicate template with html and javascript doesn''t. If render(:partial => "my_partial") includes javascript/AJX methods as well HTML is this going to cause problems with the eval()? Nicholas
eval() is a JavaScript command, and thus does _only_ take JavaScript. It fails on a mixed HTML/JavaScript input. There are some solutions to this, one of them is an addition in the upload_progress patch which provides a way to have <script> tags executed that are returned in the way you describe (normally, the browser just ignores them). Note that this solution will probably make it into Rails soon. See the patch for more: http://dev.rubyonrails.org/ticket/1026 (the ticket is heavily spammed, so try looking at this file: http://dev.rubyonrails.org/attachment/ticket/1026/ upload_progress-1388-2.diff) Note the changes to prototype.js and javascript_helper.rb. Another solution is to have a helper for generating JavaScript that includes the return of ''normal'' templates and partials. See http://dev.rubyonrails.org/ticket/933 for a possible way to do this. Thomas Am 21.06.2005 um 08:26 schrieb Nicholas Lee:> At the moment, I''m trying to figure out this method why plain text > works, but a complicate template with html and javascript doesn''t. If > render(:partial => "my_partial") includes javascript/AJX methods as > well HTML is this going to cause problems with the eval()?