【问题标题】:Deface, how to add a class to a button污损,如何向按钮添加类
【发布时间】:2012-07-04 23:20:21
【问题描述】:

美好的一天。
我想将参数“btn btn-primary”添加到 Spree 应用中的按钮。
我也尝试使用 Deface 来实现这个目标。
但是下面的代码不起作用。

app/overirdes/add_class_btn.rb

Deface::Override.new(:virtual_path => %q{spree/products/_cart_form},
                     :name => %{add_class_thumbnails_to_products},
                     :set_attributes => %q{button#add-to-cart-button},
                     :disabled => false,
                     :attributes => {:class => 'btn btn-primary'})

Spree 部分位于那里https://github.com/spree/spree/blob/master/core/app/views/spree/products/_cart_form.html.erb

结果应该在下面的图片上:
本地主机:3000/products/product1

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap overriding spree


    【解决方案1】:

    通常当覆盖对我不起作用时,这是因为我引用了错误版本的 Spree。例如,我的项目使用 spree 1.0,但我在 github 上引用了 1.1,因此我正在寻找的 data-hook 名称或我正在寻找的文件不存在。

    所以现在,我不再查看 github,而是直接查看我的项目正在使用的 Spree gem。为此,请使用bundle show spree。你可以像这样进入那个目录:

    cd my_spree_project     # make sure you're in your spree project
    cd `bundle show spree`
    

    另一个非常有用的工具是rake deface:get_result

    deface:get_result - 将列出部分的原始内容或 模板,已为该文件定义的覆盖,以及 结果标记。 get_result 接受一个参数,即 模板/部分的虚拟路径:

    运行它以验证您的覆盖实际上是否引用了某些内容。

    rake deface:get_result['spree/products/_cart_form']
    

    【讨论】:

    • rake deface:get_result 很简洁。非常感谢!
    【解决方案2】:
    Deface::Override.new(:virtual_path => "spree/products/_cart_form",
    :name => 'replace_add_to_cart_button',
    :replace => "code[erb-loud]:contains('add-to-cart-button')",
    :text => "<%= button_tag :class => 'large primary btn btn-primary', :id => 'add-to-cart-button', :type => :submit do %>",
    :original => "<%= button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do %>")
    

    这会将“btn btn-primary”标签应用于按钮。 我不知道为什么我的第一个例子不想工作。 但目标是得到。

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 2015-03-29
      • 2012-11-09
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多