简体   繁体   English

导轨中的外部链接路线

[英]External link routes in rails

Simple enough. 很简单。 When were making a typical restful rails app we would keep all routes inside of our applicaiton. 在制作典型的Restful Rails应用程序时,我们会将所有路线保留在应用程序内部。 Very rarely would a path link to an external path. 很少有路径链接到外部路径。 But if we were to do it, I'm wondering what the best way is. 但是,如果我们要这样做,我想知道最好的方法是什么。

A typical matching of home 房屋的典型搭配

match "home"=>"appcontroller#home"

If we were matching an external url to a variable of path. 如果我们将外部网址与路径变量匹配。 We might do something like the below? 我们可能会执行以下操作?

First method 第一种方法

Routes.rb Routes.rb

match "external"=>"http:/www.google.ie"

Then in our html.erb 然后在我们的html.erb中

<%= link_to 'Google', external_path %>

Note this is not actually a legal way of doing things but something similar may exist. 请注意,这实际上不是合法的处理方式,但可能存在类似的情况。 It seems very close to the current way of defining paths in rails but with an external landing. 看起来非常类似于当前在轨道中定义路径的方法,但是具有外部平台。

Second method 第二种方法

Something that I've seen done elsewhere is to create a global variable for the external URL and use it in the link. 我在其他地方看到的东西是为外部URL创建一个全局变量,并在链接中使用它。 EG. 例如。 in environment.rb or production.rb or whatever 在environment.rb或production.rb或任何

@ext_path="http:/www.google.ie"

Then in our html.erb 然后在我们的html.erb中

<%= link_to 'Google', @ext_path %>

So to recap. 因此,回顾一下。 Whats the best way to use external URLS in rails. 在rails中使用外部URL的最佳方法是什么? Paths? 路径? Variables? 变量? Other? 其他? Any input appreciated 任何输入表示赞赏

I would have kept external links only in views. 我只会在视图中保留外部链接。 Because this links are not related to the any kind of logic of the application, and it's just an UI elements. 因为此链接与应用程序的任何逻辑都不相关,而只是一个UI元素。

So, this way seems to me the best: 因此,这种方式对我来说似乎是最好的:

<%= link_to 'Google', "http://google.ie" %>

If you need to use this element many times, maybe it makes sense to bring this code into the helper, for example: 如果您需要多次使用此元素,则可以将此代码带入帮助器,例如:

def search_engine_link
  link_to 'Google', "http://google.ie"
end

And I really think that is's not very good place to introduce a more complex logic. 我真的认为引入一个更复杂的逻辑不是一个很好的地方。

I'd probably use application-wide helpers wrapped around constants. 我可能会使用围绕常量的应用程序范围内的帮助程序。

Helpers because I'd rather not see constants in templates, constants for environment-specific values. 帮助器,因为我宁愿在模板中看不到常量,也不能看到特定于环境的值的常量。

I might use a hash to store the URLs: this keeps them more-tightly-coupled, and would allow environment-wide defaults, overriding per-environment as necessary. 我可能会使用散列来存储URL:这使它们保持更紧密的耦合,并允许使用环境范围的默认值,并在必要时覆盖每个环境。

The helpers could take symbols, or be generated from the hash keys, to generate xxx_path methods as happens with routes in routes.rb . 辅助程序可以采用符号或由哈希键生成,以生成xxx_path方法,就像routes.rb路由routes.rb

I found myself needing to do this because I had a link to an external site which I intended to make local in future. 我发现自己需要这样做,因为我有一个指向外部站点的链接,我打算将来将该站点建立为本地站点。

In Rails 4 you can add this to config/routes.rb : 在Rails 4中,您可以将其添加到config/routes.rb

get '/contact', 
      to: redirect('http://www.example.com/contact.php'), 
      as: 'contact_us'

Which at the price of an extra redirect, lets you write: 通过额外重定向,您可以编写以下代码:

<%= link_to "Contact us", contact_us_path %>

301-redirects 301-重定向

A note about use of redirect(...) in the routes file. 关于在路由文件中使用redirect(...)注释。 Redirect generates a 301-redirect, which is a specific thing, Google describes it as... Redirect生成301重定向,这是特定的事情,Google将其描述为...

If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a server-side 301 redirect. 如果您需要更改页面的URL(如搜索引擎结果中所示),建议您使用服务器端301重定向。 This is the best way to ensure that users and search engines are directed to the correct page. 这是确保将用户和搜索引擎定向到正确页面的最佳方法。 The 301 status code means that a page has permanently moved to a new location. 301状态代码表示页面已永久移动到新位置。

So, using a 301-redirect in your routes for links to external websites may work, which it does, but the status information that is carried with that redirect is not correct. 因此,可以在路由中使用301重定向器来链接到外部网站,这种方法确实可以,但是该重定向器附带的状态信息不正确。

If you've moved pages in your web app and want to tell the Google index (and everyone else) about it, then by all means use the redirect() to assure Google updates the index, but it's not optimal to use for standard external links out of your web app. 如果您已在网络应用程序中移动了页面并希望将其告知Google索引(以及其他所有人),则请务必使用redirect()来确保Google更新索引,但是使用标准外部索引并非最佳选择链接出您的Web应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM