【问题标题】:How to use correctly link_to and its block in rails to incorporate html tags within?如何在rails中正确使用link_to及其块来合并html标签?
【发布时间】:2021-02-26 04:38:14
【问题描述】:

我有这段代码:

<div class="navbar navbar-expand-sm navbar-light navbar-lewagon">
  <a class="navbar-brand d-flex align-items-center" href="#">
    <%= image_tag "pokeball.png", alt: "Pokeball" %>
    <h2>Cocktails Mania</h2>
  </a>
</div>

我想将我的&lt;a&gt; 标记转换为link_to。我尝试使用此代码,但它不起作用...有人有解决方案吗?

<div class="navbar navbar-expand-sm navbar-light navbar-lewagon">
  <% link_to root_path, class: "navbar-brand d-flex align-items-center" do %>
    <%= image_tag "pokeball.png", alt: "Pokeball" %>
    <h2>Cocktails Mania</h2>
  <% end %>
</div>

谢谢

【问题讨论】:

  • 你错过了 link_to 中的 '='
  • 天哪,真是个菜鸟的错误,我的代码读了十遍……谢谢你的帮助!

标签: html ruby-on-rails


【解决方案1】:

link_to 需要被渲染,因为这个原因它没有被显示然后你错过了使它渲染的=

How do I wrap link_to around some html ruby code?

link_to Api reference

【讨论】:

  • 你有&lt;% link_to root_path。必须是&lt;%= link_to root_path
  • @Yshmarov 不是我,而是 OP,但这正是问题所在!
猜你喜欢
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 2016-12-15
  • 2011-03-19
相关资源
最近更新 更多