Difference between application.haml and application.html.haml?
Asked Answered
C

1

6

Since many days I tried to understand why a simple link like this one :

link_to 'My Link', my_path(format: :js), remote: true

was always returning full HTML document instead of executing javascript located in my file.js.erb :

alert('hello world')

[...]

After hours of debugging I found why:

When I rename my main layout file such as: application.haml it renders full HTML document :

Started GET "/my_path/2.js" for 127.0.0.1 at 2016-03-05 12:28:20 +0100
Processing by MyController#show as JS
  Rendered my_path/show.js.erb within layouts/application (0.1ms)
  Rendered layouts/_sidebar.html.erb (18.9ms)
  Rendered layouts/_headbar.haml (0.5ms)
  Rendered layouts/_flash_messages.html.haml (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 102ms (Views: 59.3ms | ActiveRecord: 2.9ms)

When I rename my main layout file such as: application.html.haml it executes javascript properly and runs my hello world popup :

Started GET "/my_path/8.js" for 127.0.0.1 at 2016-03-05 12:28:34 +0100
Processing by MyController#show as JS
  Rendered my_path/show.js.erb (0.1ms)
Completed 200 OK in 24ms (Views: 21.8ms | ActiveRecord: 0.4ms)

Why is there a difference in the javascript behavior according the different filenames of my layout ?

Commotion answered 5/3, 2016 at 11:37 Comment(2)
This is not javascript behaviour, it is how rails searches for templates. First it search for <action_name>.<templating-engine> files, then for <action_name>.<format><templateing-engine>. So when you have generic template without the format, it will be taken for all the formats.Amal
If one of view template is missing for a action, rails shall report error which tells how it is searching for it including the path, format, etc. With such info, you will know the search order.Debauch
C
2

As BroiSatse said:

This is not javascript behaviour, it is how rails searches for templates. First it search for <action_name>.<templating-engine> files, then for <action_name>.<format><templateing-engine>. So when you have generic template without the format, it will be taken for all the formats.

Commotion answered 6/3, 2016 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.