2011-01-14 36 views
10

Vì vậy, tôi có điều này:Làm thế nào để thêm một khoảng trong một link_to với một hình ảnh

<%= link_to(image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %> 

Những công trình, nhưng tôi cần một khoảng ở giữa cũng như thế này:

<a id="y_link_2" href="/pages/you/2" class=""> 
    <span>Apples</span> 
    <img src="another_small.jpg?1236340989" alt=""> 
</a> 

thế nào Tôi thêm

<span>Apples</span> 

đến link_to?

Trả lời

12

image_tagcontent_tag trở lại chuỗi cơ bản, để họ có thể được nối bằng cách sử dụng + điều hành một cách dễ dàng:

<%= link_to(content_tag(:span, "Apples") + image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>

Tuy nhiên, như bạn thấy, nó được khá lộn xộn - có thể là giá trị chuyển nó thành một phương pháp trợ giúp.

17

ăn một khối để gọi link_to của bạn:

<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %> 
    <%= content_tag(:span, 'Apples') %> 
    <%= image_tag(@model.picture.url(:thumb), :alt => '') %> 
<% end %> 

Hoặc:

<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %> 
    <span>Apples</span> 
    <%= image_tag(@model.picture.url(:thumb), :alt => '') %> 
<% end %> 
0

Haml vay cũng chính nó để những tình huống như thế này. Ví dụ:

= link_to "/pages/you/#{something.id}", id: "y_link_#{something.id} do 
    %span Apples 
    = image_tag(@model.picture.url(:thumb), alt: '') 

Alternatively:

%a[something, 'y_link']{href: "/pages/you/#{something.id}"} 
    %span Apples 
    %img{src: @model.picture.url(:thumb)} 

Đó là giá trị học tập nếu bạn muốn viết ý kiến ​​của bạn nhanh hơn và đọc chúng tốt hơn.

0

Để có đường dẫn, hãy sử dụng cấu trúc như vậy

<%= link_to edit_section_path(@section) do %> 
    Edit 
    <span class="fa fa-list pull-right"></span> 
<% end %> 
Các vấn đề liên quan