了解 Haml 代码
Understanding Haml Code
我目前正在处理一个 RoR 项目,我的团队在该项目中使用 Haml 作为模板引擎。我很难理解以下代码。
%td
= link_to 'Edit', [:edit, :merchants, category, subcategory], class: 'btn'
= link_to 'Delete', [:merchants, category, subcategory], :method => :delete, :data => { :confirm => 'Are you sure?' }, class: 'btn'
其中 html 翻译成
<a class="btn" href="/admin/categories/28/sub_categories/147/edit">Edit</a>
<a class="btn" data-confirm="Are you sure?" data-method="delete" href="/admin/categories/28/sub_categories/147" rel="nofollow">Delete</a>
从 Haml documentation, 我发现 [] 符号用于对象引用,所以我不确定 [ ] 符号如何转换为 href 符号。我对 Ruby On Rails 和 Haml 都很陌生,因此我们将不胜感激。
与 Haml
无关。它是一个 Rails
助手,它从 :symbol
和 variables
的数组构建 some_awesome_path
。
在内部,Rails
会将 [:edit, :merchants, category, subcategory]
转换为对 merchants_category_subcategory_path
的调用。大多数(如果不是全部)Rails
需要路径的方法也会采用资源的对象表示,例如 respond_with
、link_to
、render
、redirect_to
, form_for
, 等等
一些例子:
这个数组:
[:new, @object, :post]
翻译成:
new_businesses_post_path()
和 new_businesses_post_url()
在Rails中我们有两种构建路径的方法
- 通过
*_path
方法
- 通过多态路径 http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html#method-i-polymorphic_url
还有一些方法(form_for
、link_to
)不需要调用polymorpic_path
并且可以直接获取数组。
我目前正在处理一个 RoR 项目,我的团队在该项目中使用 Haml 作为模板引擎。我很难理解以下代码。
%td
= link_to 'Edit', [:edit, :merchants, category, subcategory], class: 'btn'
= link_to 'Delete', [:merchants, category, subcategory], :method => :delete, :data => { :confirm => 'Are you sure?' }, class: 'btn'
其中 html 翻译成
<a class="btn" href="/admin/categories/28/sub_categories/147/edit">Edit</a>
<a class="btn" data-confirm="Are you sure?" data-method="delete" href="/admin/categories/28/sub_categories/147" rel="nofollow">Delete</a>
从 Haml documentation, 我发现 [] 符号用于对象引用,所以我不确定 [ ] 符号如何转换为 href 符号。我对 Ruby On Rails 和 Haml 都很陌生,因此我们将不胜感激。
与 Haml
无关。它是一个 Rails
助手,它从 :symbol
和 variables
的数组构建 some_awesome_path
。
在内部,Rails
会将 [:edit, :merchants, category, subcategory]
转换为对 merchants_category_subcategory_path
的调用。大多数(如果不是全部)Rails
需要路径的方法也会采用资源的对象表示,例如 respond_with
、link_to
、render
、redirect_to
, form_for
, 等等
一些例子:
这个数组:
[:new, @object, :post]
翻译成:
new_businesses_post_path()
和 new_businesses_post_url()
在Rails中我们有两种构建路径的方法
- 通过
*_path
方法 - 通过多态路径 http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html#method-i-polymorphic_url
还有一些方法(form_for
、link_to
)不需要调用polymorpic_path
并且可以直接获取数组。