I'm use the remote: true idiom from the Working with Javascript in Rails guide:
# new.html.slim = form_for @thing, remote: true do |f| f.text_field :whatever f.submit 'Submit' # thing_controller.rb layout 'foo' def create end # create.js.erb alert('foobar') This fails, because create.js.erb is for some reason rendered inside the 'foo' layout and returned as html, not javascript, despite the fact that the request is correctly processed as Javascript:
Processing by ThingsController#create as JS Parameters: {"utf8"=>"✓", "commit"=>"Submit"} Rendered things/create.js.erb (0.6ms) (The problem is the same whether or not I have an explicit respond_to format block in the controller action.)
As noted here and here, including render layout: false in the controller action fixes the problem:
# thing_controller.rb layout 'foo' def create render layout: false end But why do I need render layout: false here? Why is Rails rendering the javascript inside an html layout? I'm particularly puzzled because I've used the same idiom in several other places and never run into this problem before.