Rails如何将link_to用于外部网站并使用块?

问题描述 投票:1回答:1

我正在尝试使用link_to与块,并与外部网站链接,我已经知道如何使用link_to使我的div可点击(learned from here),并使用link_to将用户发送到另一个网站(learned from here),但当我尝试结合这个两个approches我收到一个错误:

undefined method 'stringify_keys' for "www.google.com":String

这是我的html.erb代码:

<%= link_to @slides[0].link, "#{@slides[0].link}", target: "_blank"  do %>
  <div class="carousel-item active">
    <%= image_tag @slides[0].slide_image.thumb.url, class: "d-block w-100", alt: @slides[0].image_text %>
    <div class="carousel-caption d-none d-md-block">
      <h5><%= @slides[0].image_text %></h5>
    </div>
  </div>
<% end %>

我也尝试过:

<%= link_to @slides[0].link do %> # or
<%= link_to "#{@slides[0].link}", :target => "_blank" do %>
# I got the same error

<%= link_to url_for(@slides[0].link) do %>
# above I got localhost:3000/https://google.com insted of https://google.com

有谁知道如何做到这一点?

ruby-on-rails ruby ruby-on-rails-5 erb link-to
1个回答
2
投票

带块的link_to的工作原理如下:

link_to(url, html_options = {}) do
  # block
end

link_to helper source

所以你必须这样做:

<%= link_to @slides[0].link, target: "_blank"  do %>
  #block
<% end %>

(假设@slides[0].link == "https://google.com"即有效的外部网址)

© www.soinside.com 2019 - 2024. All rights reserved.