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?
Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.
Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates. Partials are also useful when you need to reuse exactly the same code (DRY philosophy).
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 a js
action if no js
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
:
# yourviews/show.html.erb <div>Content!</div> # layouts/fadein.js.erb $("#main").fadeIn("<%= escape_javascript(yield) %>"); # YourController.rb def show # ... respond_to do |wants| wants.html wants.js { render :layout => "fadein" } end end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With