Last Updated: February 25, 2016
·
2.652K
· jalada

Rails: Why are my JS views rendering with my HTML layout wrapped around them?

In my Rails 3 controller I had this simple piece of code:

respond_to do |format|
  format.js
  format.html do
    render layout: "embed"
  end
end

I couldn't understand why when I called the action with format set to js my Javascript view would be rendered with my standard HTML application layout wrapped around it.

Turns out that at some point something or someone had given my application layout the filename application.haml instead of application.html.haml. This made it apply to all formats instead of just HTML.

Once I renamed the layout, the problem went away!