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? :stuck_out_tongue:

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