2011-06-22 40 views
4

嗨我想要創建圖像的按鈕。所以基本上,我需要以下代碼的button_to版本:|按鈕與圖像?

<%= link_to image_tag(product.image_url, :class => "img"), line_items_path(:product_id => product) %>  
+0

請解釋什麼是問題,並提供有關您得到的錯誤信息 –

回答

0

您可以創建一個輔助的button_to鏈接 -

<%= button_to product.image_url, line_items_path(:product_id => product) %> 

和application_helper

def button_to(image_path, link) 
    link_to (image_tag(image_path, :class => "img"), link) 
end 

我想這是你想要的。

+0

這一個創建鏈接,而不是一個按鈕。 IM建議通過CSS –

+1

一個按鈕只能用於提交或重置表單創建按鈕和風格吧。如果你只是想的樣子,那麼你可以風格標記一下這種方式。 – rtdp

+0

最後的評論是否準確?我已經非常頻繁地將非提交行爲附加到按鈕標記上...... – jaydel

1

這是我的解決方案:

使用按鈕幫助(你可以使用button_to helper方法):

<%= f.submit 'Save', :class => "button_with_image_save" %> 

CSS:

.button_with_image_save { 
    background: url(../images/icons/page_save.png) #f2efa8 no-repeat 10px 6px; 
    text-indent:30px; 
    display:block; 
    cursor: pointer; 
} 
0

圖片提交按鈕:

<%= image_submit_tag("team/team1.png", class: 'image-responsive') %> 

鏈接與圖片:

<%= link_to(image_tag("team/team1.png", class: 'image-responsive'), root_path, :method => :get) %> 
1

這是一個很老的文章,但對於未來的參考:因爲Rails的3.2。 1可以使用button_tag代替button_to,如在第一本地一個允許圖像:

創建定義了提交按鈕,復位按鈕或一個通用按鈕,可以在JavaScript中使用,例如一個按鈕元件。您可以將按鈕標記用作常規提交標記,但在舊版瀏覽器中不支持。然而,按鈕標籤允許更豐富的標籤,如圖像和重視,所以這個助手也將接受一個塊。

至於你的例子:

<%= button_tag image_tag(product.image_url), line_items_path(:product_id => product), class: 'img' %> 

我沒有測試的代碼,但它應該工作。這是可能的,你需要聲明的網址url: