21

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.

6 Answers 6

30

Well, this worked for me in Rails 4.0:

render :text => "", :layout => true 
Sign up to request clarification or add additional context in comments.

2 Comments

This seems like just a hacky way of rendering the layout with no input
This will throw a deprecation warning in Rails 5. You can use render html: '', layout: true to achieve the same result.
20

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

1 Comment

Using Rails 5.1, this worked for me, whereas the accepted answer only worked if I passed layout: false, otherwise it rendered my layout html twice. Passing nil instead of an empty string to the html key also worked, which seems slightly cleaner to me.
5
render :file => "layout_file", :layout => false 

1 Comment

That's an interesting option as well. I wonder if there's a downside to rails not thinking it's a layout.
4

Try this, this will render the response to a file called sample.html which could be a static html file.

and also you could have this file in a common location, so that you could loaded it to all the actions

have your static content in this page, and if you need a dynamic page you could have a .erb page too

in your method

def index @posts = Post.all respond_to do |format| format.html {render :file => "posts/sample"} format.json { render json: @posts } end end /post/sample.html 

HTH

3 Comments

My concern with that is returning unneeded data. I thought about :text => "" as an option as well, but again you need :layout => true as by default it renders text only.
cant u have a blank static html page with a message (or something), or could u explain what the user needs to see when he hits this action ! (or have all the css and js in the static file and with out using a layout at all)
That could work as well :). A co-worker of mine was also suggesting doing the front end aspect in sinatra, and have rails be the pure API.
4

I needed something similar-- to display a layout template with no partial (where partials are handled clientside).

rails 4.2.0

respond_to do |format| format.html { render text: nil, layout: true } end 

1 Comment

While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.
0

I believe if your application is just fetching JSON from the server, the format should be json and not html.

respond_to do |format| format.json @your_collection end 

Or put the format for all actions in your controller and just respond with the objects.

6 Comments

The problem with that is responding to regular URLs. I dont want to duplicate haml & JST views. Let's say i have a simple blog with a posts controller. So to access a post directly, you'd go to /posts/1. So what that needs to do is render the layout. If i just respond with JSON, and i dont say something for the html response, then /posts/1 throws a template error.
Hmm, i tought you would handle the DOM code with just javascript. Like replacing the html based on the json from the server.
You can. But then it gets trickier for when trying to replace content via link clicks and what not. When an app gets larger it gets complicated. Im trying to get around this mindset, and get some practice with this style, so i can grow this app into something.
I am not sure about your application infrastructure, but i recommend you to take a look at the new and hyped Javascripts MVC frameworks which are many these days. (Backbone.js, Ember.js, Angular.js). Most of them have a great interaction with Rails and could possibly make this kind of task more easy.
Yup that's what im doing. Just didnt mention it as it doesn't really effect the question. Ive used things like rails backbone ontop of a regular rails g scaffold, and that works pretty nicely. I just dont want double views, I want to see how this goes as an experiment :D
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.