Ruby-on-rails 如何将变量 link_to 定义为外部 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16721816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:02:28  来源:igfitidea点击:

How do I defined a variable link_to to an external URL

ruby-on-railsruby-on-rails-3link-to

提问by Fawyd

On my site a user has a personal profile with a link to his personal external website. The url of the sites I store in a postgresql database under the name website. When I test the result, I always get a url like this:

在我的网站上,用户有一个个人资料,其中包含指向其个人外部网站的链接。我以网站名称存储在 postgresql 数据库中的站点的 url 。当我测试结果时,我总是得到这样的网址:

http://localhost:3000/www.example.com

instead of http://www.example.com

代替 http://www.example.com

My view index.html.erblooks like this:

我的视图index.html.erb看起来像这样:

<% provide(:title, 'All projects') %>
<h1>All projects</h1>

<%= will_paginate %>

<ul class="microposts">
    <%= render @microposts %>
</ul>

<%= will_paginate %>

and my _micropost.html.erblike this:

和我的_micropost.html.erb像这样:

<li>
    <span class="title"><%= micropost.title %></span>
    <span class="website"><%= link_to micropost.website, micropost.website %></span>
    <span class="content"><%= micropost.content %></span>
    <span class="timestamp">
        Posted <%= time_ago_in_words(micropost.created_at) %> ago.
    </span>
</li>

I don't know what's the problem in this case. If I set a @ before micropost.websiteit gives me an error undefined method `website' for nil:NilClass

我不知道在这种情况下有什么问题。如果我在micropost.website之前设置 @,它会给我一个错误undefined method `website' for nil:NilClass

Does anyone can help me (I'm a RoR beginner)?

有没有人可以帮助我(我是 RoR 初学者)?

KR, Fabian

KR,法比安

回答by Goro

It sounds like you are storing URLs without the http://so they are being interpreted as relative URLs. You just need to do something like this:

听起来您正在存储没有 的 URL,http://因此它们被解释为相对 URL。你只需要做这样的事情:

link_to micropost.website, "http://#{micropost.website}"

or maybe add a full_urlmethod to that model that adds it if it's missing.

或者可能full_url向该模型添加一个方法,如果它丢失,则添加它。

By the way, you can't use @micropostin that partial because it doesn't exist (you only have @micropostsor micropost).

顺便说一句,您不能@micropost在该部分中使用,因为它不存在(您只有@micropostsmicropost)。

回答by Dimang Chou

You can try with this below code:

您可以尝试使用以下代码:

<%= link_to "your label", "your link with http", :target => "_blank" %>

<%= link_to "your label", "your link with http", :target => "_blank" %>

This will create a link that opens in a new tab.

这将创建一个在新选项卡中打开的链接。

回答by s.vatagin

You can do something like that:

你可以这样做:

link_to micropost.website, url_for(micropost.website)

See Rails Api: url_for

参见Rails Api:url_for

You can experiment in rails console. Just type in console:

您可以在 rails 控制台中进行实验。只需在控制台中输入:

micropost = Micropost.first
helper.link_to micropost.website, url_for(micropost.website)

And you see a result string.

你会看到一个结果字符串。

Also you need to learn the difference between path and url helpers. See ruby on rails guide.

您还需要了解路径和 url 助手之间的区别。请参阅ruby on rails 指南。

Goro rights. You need to add "http://" to your website attribute. After validating and before save Model instance to database you need to add this prefix.

五郎权利。您需要将“http://”添加到您的网站属性中。在验证之后和将模型实例保存到数据库之前,您需要添加此前缀。

回答by Eric Norcross

You can use the ruby URI class

您可以使用 ruby URI 类

= link_to micropost.website, URI::HTTP.build({:host => micropost.website}).to_s, target: "_blank"

# <a target="_blank" href="http://www.example.com">www.example.com</a>

回答by alexventuraio

I'm working with Rails 5 and I had the same problem. All I need to do to fix it, was to include the protocol on my link_totag. E.g. I had www.google.com.mx, then, it should be http://www.google.com.mx. And that's it it works just fine like in the official docis mentioned.

我正在使用 Rails 5,但遇到了同样的问题。我需要做的就是修复它,就是在我的link_to标签上包含协议。例如,我有www.google.com.mx,那么,它应该是http://www.google.com.mx。就是这样,它就像在官方文档中提到的那样工作得很好。

So, finally I just have something like this in my view:

所以,最后我只有这样的事情:

<%= link_to (content_tag(:i, "help", class: 'material-icons tiny')), " http://www.google.com.mx", target: "_blank", rel: "alternate" %>

Which is the same as:

这与以下内容相同:

<%= link_to "help", "http://www.google.com.mx", target: "_blank", rel: "alternate" %>

I hope it helps somebody else.

我希望它可以帮助别人。

回答by Linus Oleander

I use the postrank-urigem to normalize the url before passing it to link_to.

我使用postrank-urigem 在将 url 传递给link_to.

class User < ActiveRecord::Base
  def normalized_webpage
    webpage && PostRank::URI.normalize(webpage).to_s
  end
end

Then you can use link_to "website", user.normalized_webpage, target: "_blank"in your view. This will for example add the http://to the url, if it's missing.

然后你可以link_to "website", user.normalized_webpage, target: "_blank"在你的视图中使用。例如,这将添加http://到 url,如果它丢失。

回答by Linus Oleander

Here's what i did.

这就是我所做的。

Let's say we have @person and he has a link (@person.link) # => www.google.com

假设我们有@person 并且他有一个链接 (@person.link) # => www.google.com

in your helpers create something like this

在你的助手中创建这样的东西

def extlink(link)

 if link.include?("http://")
  puts link
 else
  link.insert(0, "http://")
  link
 end

end

结尾

And in your file you can do

在你的文件中你可以做

<% @person.each do |p| %>

<%= link_to 'External', extlink(p.link) %>

<% end %>

Works for me

对我来说有效