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

Rails how to use link_to for an external website and also using a block?

我正在尝试将 link_to 与块一起使用,并且还尝试将 link 与外部网站一起使用,我已经知道如何使用 link_to 来制作我的 div可点击 (learned from here) and also to use link_to to send user to another website (learned from here) 但是当我尝试结合这两种方法时出现错误:

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

有人知道怎么做吗?

带块的 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" 即有效外部 url)