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?
This is not a good practice as you can see by the comments. Rails have the concepts of view, partial and layout. That said, the view is the only one you should keep using only once. So my suggestions are:
Like others have mentioned, rendering another non-partial view in another view is "Not the rails way", if however you still insist on doing it, one method is:
you just need to pass a context to your
render
method:Rendering a non-partial view inside another view isn't exactly the Rails Way™. Your current solution is probably better, and not an uncommon approach. Rename it _body, or something else appropriate, if you feel weird about the the partial name being the same as the action.
However if your view can be shared, as it seems like it could in this case, you could just make it a layout.
This is facilitated by the fact that, somewhat against the principle of least surprise, Rails will render an
html
template for ajs
action if nojs
template exists. This means that you could remove both the js template, and the partial, and just create a layout entitled, for example,fadein.js.erb
: