2012-07-04 17 views
2

美好的一天。
我想將參數'btn btn-primary'添加到Spree應用程序中的按鈕。
我也嘗試爲此目標使用Deface。
但下面的代碼不起作用。Deface,如何給按鈕添加一個類

應用程序/ 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'}) 

施普雷部分位於那裏https://github.com/spree/spree/blob/master/core/app/views/spree/products/_cart_form.html.erb

和結果應當是在下面的圖片:
本地主機:3000 /產品/產品1

btn button div

回答

1
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-主' 標籤按鈕。 我不知道爲什麼我的第一個例子不想工作。 但是,目標是得到。

3

通常,當一個覆蓋不適用於我時,這是因爲我引用了Spree的錯誤版本。例如,我的項目使用了spree 1.0,但是我在github上引用了1.1,所以我尋找的data-hook名稱或我尋找的文件都不存在。

所以,現在,而不是檢查github,我直接看我的項目正在使用的施普雷寶石。要做到這一點使用bundle show spree。你可以轉到如下目錄:

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

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

抹掉:get_result - 將列出部分或 方案的原始內容,已經爲該文件中定義的覆蓋,並 隨之產生的標記。 get_result接受一個參數,它是模板/分的 虛擬路徑:

運行它來驗證您的覆蓋實際上是參考的東西。

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

我喜歡cd'bundle show spree'。整齊! – Lavixu

+1

rake deface:get_result是整潔的。萬分感謝! – Lavixu

相關問題