Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html.haml vs haml in Rails view templates

What is the difference between using view templates in Rails as '.html.haml' and as simply '.haml'? For example,
show.html.haml
vs
show.haml

Are there any particular advantages of using one over the other? Or is one of them a standard practice? The same question goes for other types of templates as well, like '.erb'.

like image 797
Vignesh Avatar asked Jan 31 '12 06:01

Vignesh


1 Answers

The format matters when you're using Rails' automatic responders. See http://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_with.

So for example, with:

class CarsController < ActionController::Base
  respond_to :html, :js

  def index
    respond_with Car.all
  end
end

The app will render app/views/cars/index.html.erb and app/views/index.js.erb when viewing http://localhost:3000/cars and http://localhost:3000/cars.js, respectively.

Additionally, this is an established convention in Rails-land, so it's just a good idea to do this anyway, even if you're not using the auto-responders.

like image 62
jtrim Avatar answered Sep 22 '22 14:09

jtrim