Rails视图模板中的html.haml与haml

Vig*_*esh 7 html haml ruby-on-rails

在Rails中使用视图模板作为'.html.haml'和简单地'.haml'之间有什么区别?例如,
show.html.haml
vs
show.haml

使用一个优于另一个有什么特别的优势吗?或者其中一个是标准做法?同样的问题也适用于其他类型的模板,例如'.erb'.

jtr*_*rim 5

当您使用Rails的自动响应程序时,格式很重要.请参阅http://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_with.

例如,用:

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

  def index
    respond_with Car.all
  end
end
Run Code Online (Sandbox Code Playgroud)

该应用程序将呈现app/views/cars/index.html.erbapp/views/index.js.erb观看时http://localhost:3000/carshttp://localhost:3000/cars.js分别.

另外,这是Rails-land中的既定惯例,所以即使您没有使用自动响应器,最好还是这样做.

  • 这是否意味着使用“.html.haml”而不是“.haml”是首选和标准的? (2认同)
  • @jtrim 感谢您澄清这一点,但我认为既定的惯例和一个好主意并不是一个论点。我想确切地知道为什么当我不使用自动回复器时应该遵循 html.haml 命名约定。据我了解,rails 4 中不再有响应程序(respond_to、respond_with),它已被提取到外部 [responders gem](https://github.com/plataformatec/responders)。 (2认同)