如何在带有图像的 link_to 中添加跨度

发布于 2024-10-11 23:14:53 字数 617 浏览 6 评论 0原文

所以我有这个:

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

哪个有效,但我也需要像这样的跨度:

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

如何添加

 <span>Apples</span>

link_to

So I have this:

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

Which works, but I need a span in between also like this:

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

How do I add

 <span>Apples</span>

to the link_to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒中人 2024-10-18 23:14:53

向您的 link_to 调用提供一个块:

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

或者:

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

Feed a block to your link_to call:

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

Alternatively:

<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
  <span>Apples</span>
  <%= image_tag(@model.picture.url(:thumb), :alt => '') %>
<% end %>
风蛊 2024-10-18 23:14:53

image_tagcontent_tag 返回基本字符串,因此可以使用 + 运算符轻松连接它们:

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

但是,正如您所看到的,它变得非常混乱 - 可能值得将其移动到辅助方法中。

image_tag and content_tag return basic strings, so they can be concatenated using the + operator easily:

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

However, as you can see, it gets quite messy - might be worth moving it into a helper method.

百合的盛世恋 2024-10-18 23:14:53

对于路径,请使用如下结构

<%= link_to edit_section_path(@section) do %>
   Edit
   <span class="fa fa-list pull-right"></span>
<% end %>

For a path, use the structure like so

<%= link_to edit_section_path(@section) do %>
   Edit
   <span class="fa fa-list pull-right"></span>
<% end %>
廻憶裏菂餘溫 2024-10-18 23:14:53

Haml 非常适合此类情况。例如:

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

或者

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

如果你想写下你的观点,值得学习更快并更好地阅读它们。

Haml lends itself well to situations like these. For example:

= 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)}

It's worth learning if you want to write your views faster and read them better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文