【问题标题】:Understanding Haml Code理解 Haml 代码
【发布时间】:2015-05-01 11:29:44
【问题描述】:

我目前正在处理一个 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 都很陌生,因此我们将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 haml


    【解决方案1】:

    Haml 无关紧要。它是一个 Rails 助手,从 :symbolvariables 的数组构建 some_awesome_path

    在内部,Rails 会将[:edit, :merchants, category, subcategory] 转换为对merchants_category_subcategory_path 的调用。大多数(如果不是全部)Rails 期望路径的方法也将采用资源的对象表示形式,例如 respond_withlink_torenderredirect_toform_for 等。

    一些例子:

    这个数组:

    [:new, @object, :post]
    

    翻译成:

    new_businesses_post_path()new_businesses_post_url()

    Creating URLs From Objects

    【讨论】:

      【解决方案2】:

      在 Rails 中,我们有两种构建路径的方法

      还有一些方法(form_forlink_to)不需要调用polymorpic_path,可以直接获取数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 1970-01-01
        • 2018-05-23
        • 2015-02-25
        • 2012-01-14
        • 1970-01-01
        相关资源
        最近更新 更多