Just trying out a simple rails app, mostly going for an API backend with JSON, heavy client side app. So what i want to do is only render the layout, and have javascript code handle the url, and make the ajax request to get the json data. The following seems to work:
respond_to do |format| format.html { render :nothing => true, :layout => true } end
However, since nothing is meant to render nothing, it feels kinda wrong. Is there a more proper way to just render the layout? Note that my layout does not have a yield.
Well, this worked for me in Rails 4.0:
render :text => "", :layout => true
(Copying Vasile's comment for better visibility.)
For rails 5.1, in order to render the layout without requiring a view template, you need to use
def index render html: '', layout: true end
or with a custom layout
def index render html: '', layout: 'mylayout' end
Per tfwright, this will also work:
def index render html: nil, layout: true end
However, the following will not work:
render text: '', layout: 'mylayout'
will give you an error because the view template index.html.haml
does not exist.
render nothing: true, layout: 'mylayout'
will give you an error because nothing:true
is deprecated, and the index
template does not exist (however, this works in rails 4.2)
render body: '', layout: 'mylayout'
will render '' (no layout)
render plain: '', layout: 'mylayout'
will render '' (no layout)
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