katchwreck
2012-Aug-24 09:37 UTC
question regarding proper use of render :js for handling AJAX requests
Dear rails gurus,
Is this a proper way to write the following render :js argument inside a
controller for handling a simple AJAX response (div update) to a JS request
using rails 3.2.1?
render :js => ''$("span#main").html("'' +
escape_javascript(File.read(''app/views/main/my_file.html.erb''))
+ ''");''
While this code is working, it seems a little clunky e.g. the File.read
call feels out-of-place somehow... but then again, what do I know? :-P
Thanks for your comments/advice! The main reason why I am doing this is as
a shortcut to avoid creating a .js.erb file, where I would have something
like:
escape_javascript(render template:''main/my_file.html.erb'')
instead; trying to run this inside of the controller gave me a couple
issues:
1. the "render template" returned a length-1 array instead of a
string,
which I had to reference the 0th-element of in order to get
escape_javascript not to throw a "gsub on Array" error
2. can''t use render twice in the same line (double redirect error) led
me
to the alternative "File.read..." idea
Forgive me if this question has been posted before, and thank you all in
advance for your kind assistance.
Danny
@katchwreck
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/tR58qAKFBSYJ.
For more options, visit https://groups.google.com/groups/opt_out.