Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails rendering layout only?

Tags:

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.

like image 262
agmcleod Avatar asked Jan 11 '13 16:01

agmcleod


2 Answers

Well, this worked for me in Rails 4.0:

render :text => "", :layout => true 
like image 200
Devaroop Avatar answered Dec 12 '22 19:12

Devaroop


(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)

like image 29
MaximusDominus Avatar answered Dec 12 '22 18:12

MaximusDominus