I have a controller that responds to both html and js. The html view renders the whole page (including the header and footer), while the js only replaces #main. Aside from the header and footer, both formats render the same content. I can get this effect with three files:
_show.html.erb
<div>Content!</div>
show.html.erb
<%= render "show" %>
show.js.erb
$("#main").fadeIn("<%= escape_javascript(render 'show') %>");
This works, but I'd prefer if I didn't need a separate _show partial. Unfortunately, this doesn't work:
show.html.erb
<div>Content!</div>
show.js.erb
$("#main").fadeIn("<%= escape_javascript(render 'show') %>");
As Rails will look for the show partial, not the actual view.
Is there a way to get Rails to look for the view file, rather than a partial?
:fileoption, e.g.:file => 'show'. The render helper will implicitly turn the string argument ofrender 'yourview'intorender :partial => 'yourview'. The problem you're going to run into, however, is that the lookup context in the js action is going to resolve "show" to the js version, probably resulting in an infinite loop or some such.missing templateerror. Looks like it can't even find itself._bodyif you feel weird about the naming. The common thing you're going to see in this scenario isrender(@resource), which turns into the partial path e.g.resources/_resource.lookup_context.update_details(:formats => [:html]) { @content = render_to_string }. Then in the js view you couldrender(@content). However that was Rails 3.0.x, and probably not a clean solution even then.