2012-06-01 27 views
0

對於我的生活,我無法弄清楚爲什麼會出現這個問題。我一直使用link_to helper,但是iv只使用了link_to_if helper幾次,這次我無法獲得鏈接來獲取CSS類。Rails link_to_if類不能正確顯示

這裏是我的鏈接

<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => @test_script), { :class => 'btn btn-mini' })%> 

的圖像顯示,與沒有鏈接如預期,但最後定義的CSS類沒有,相反,它只是沒有類。這與我在所有link_to助手中使用的格式相同。

任何人都可以解釋爲什麼嗎?

回答

2

使用link_to_if,只有名稱 - 在您的情況下raw('<i class="icon-chevron-up"></i>')的結果 - 將在條件失敗時返回。否則將應用於鏈接標記的所有選項都將被忽略。

請參閱source

0

嘗試將空散列作爲選項參數傳遞,以便{ :class => '...' }被分配給html_options參數。未經測試

<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => @test_script), {}, { :class => 'btn btn-mini' })%> 
+0

謝謝,但它沒有奏效。我得到:錯誤的參數數量(4爲5) – Cheyne

0

包裹link_to_iflink_to_unless在跨度:

%span.pull-right 
    = link_to_unless Foo.deleted.empty?, "<i class='icon-white icon-trash'></i> Undelete Foo".html_safe, deleted_foos_path 

上面代碼中設置一個CSS類(右拉)上無論是顯示 - 全鏈路或只是其文本。