3

Say I have an application#search method that I would like to respond to AJAX requests. Inside of my JS view (app/views/application/search.js.erb) I would like to render the HTML view:

$("#content").html( escape_javascript( render( :action => 'search', :format => 'html' ) ) );
$("#title").html( "Title" );

I know I could move things into a partial and render that in both the HTML and JS views, but the problem is I have many different actions that need to respond in this way and I'd prefer not creating a bunch of 1-line view files that just render a partial.

Is there any way to explicitly render an action with a specific format and grab the different parts of content_for as well? So for the above example, I could set #title to the HTML returned with content_for :title or something.

John Bachir
  • 22,495
  • 29
  • 154
  • 227
lyricat
  • 1,988
  • 2
  • 12
  • 20
  • have you tried this: http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to – corroded Jul 03 '11 at 04:41
  • what was the result of this? I'm trying to render html similarly using `escape_javascript( render :action_name, :format => 'html' )` too inside a JS view – mswieboda Feb 12 '14 at 17:27
  • Could you expand on your issue a bit please... The syntax in your example is slightly wrong. `$('#content'). html(escape_javascript('<%= render action: 'search', format: 'html' %>'));` http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-escape_javascript – ChuckJHardy Feb 17 '14 at 17:21
  • 2
    The answer is to use `:formats => ['html']` instead of `:format => 'html'`. See here: http://stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails This should be fixed in newer versions of rails, see here: https://github.com/rails/rails/issues/4841 – Samuel Apr 16 '14 at 14:33

1 Answers1

1

I'm not sure outputting a bunch of HTML into JS source is a great design, but that wasn't your question -- and I've had to do similar things to output HTML snippets rendered by templates into XML, which I consider more reasonable :).

Anyhow, there are several things in your question.

It changes from version to version of Rails, but in Rails 4.x, I think this should work, you were close, but it for some reason needs to be :formats (plural) with an array. And there were some other unrelated things not right in your example, like the need for ERB <%= tags, and putting javascript strings into quote marks.

var str = "<%= escape_javascript( render( :partial => 'partial_name', :formats => ['html'] ) ) %>";

I think. I haven't tried that exactly myself.

Note that's still a partial though. You want to render a full (not partial) template from inside another template -- I'm pretty sure Rails won't let you do that. It's been a bit annoying to me before too. But in fact, even to render an HTML partial ("something.html.erb") from a JS partial, you need to use the :formats => [] trick, or Rails will insist "hey, I want to find a js partial with that name, we're in js mode man!".

But as above should render _partial_name.html.erb from a JS template. Which is still probably not a great design.

Hope this helps.

jrochkind
  • 22,799
  • 12
  • 59
  • 74