简体   繁体   English

Ruby on Rails的link_to方法

[英]Ruby on Rails link_to method

I'm trying to use the link_to method in my navigation bar but i'm clearly lacking the fundamentals to understand how it actually works. 我正在尝试在导航栏中使用link_to方法,但是我显然缺乏了解其实际工作原理的基础知识。

<% link_to "Viral", @viral %>

I'm under the impression that the "Viral" is the label for the link, however i have no idea what @viral is. 我的印象是“病毒”是链接的标签,但是我不知道@viral是什么。 Is that route link to a controller / view (ie to the viral controller & view), and if so, what would the route look like? 该路由是否链接到控制器/视图(即,病毒控制器和视图),如果是,路由将是什么样? I'm in the process of trying to create category pages for different articles and felt this would be the most obvious way for navigation. 我正在尝试为不同的文章创建类别页面,并认为这将是最明显的导航方式。

By Rails conventions, link_to converts a model instance into an URL which links to the correspondent and RESTful show action. 根据Rails的约定, link_to将模型实例转换为URL,该URL链接到相应的RESTful show操作。

So in your example 所以在你的例子中

<%= link_to "Viral", @viral %>

is equivalent to 相当于

<%= link_to "Viral", viral_path(@viral) %>

You will get more information in the Docs . 您将在“ 文档”中获得更多信息。

@viral is simply the path. @viral只是路径。

If you have a route defined like so: 如果您有这样定义的路线:

get "viral", as: "something#viral" => "viral"

then you should put <%= link_to "some text", viral_path %> and the link will automatically direct to www.domain.com/viral. 那么您应该在<%= link_to "some text", viral_path %>链接,该链接将自动定向到www.domain.com/viral。

If you have a specific url in mind, you should instead write in quotes the url AFTER the domain. 如果您有特定的网址,则应在域后用引号将该网址引起来。 So if your domain is www.domain.com and you want to redirect to www.domain.com/viral, you would write 因此,如果您的域名是www.domain.com,而您想重定向到www.domain.com/viral,则应编写

<%= link_to "some text", "viral" %>

@viral is a variable (object) @viral是变量(对象)

link_to is a helper method what make html link link_to是使HTML链接有效的辅助方法

<%= link_to "Viral", @viral %>

will build link to show action, but if you need to set another path just use: 将建立链接以显示操作,但是如果您需要设置其他路径,请使用:

<% link_to "Viral", viral_path %>

all paths you can see in terminal 您可以在终端中看到的所有路径

rake routes

Your link_to needs to have the following passed to it: 您的link_to需要传递以下信息:

<%= link_to "Link Text", "http://link-path.com" %>

The first argument is the anchor text, the second is the URL the browser will request. 第一个参数是锚文本,第二个参数是浏览器将请求的URL。


Since Rails is an application framework, you can pass dynamic values to the link_to arguments. 由于Rails是一个应用程序框架,因此可以将动态值传递给link_to参数。 Something like... 就像是...

#app/helpers/application_helper.rb
def your_link_helper
   "http://google.com"
end

<%= link_to "Link Text", your_link_method %>

If you understand this, you'll understand that Rails sets a series of "helper" methods for your "routes" , which means that instead of your_link_method (above), you'll be able to use... 如果理解了这一点,您将了解Rails为“ routes”设置了一系列“ helper”方法 ,这意味着您可以使用...而不是your_link_method (上面)。

#config/routes.rb
resources :virals

<%= link_to "Viral", viral_path("12") %>

- -

So... 所以...

@viral is an instance variable - a variable set during an instance of a class (in this case, your controller): @viral是一个实例变量 -在类实例(在本例中为您的控制器)的实例期间设置的变量:

#app/controllers/virals_controller.rb
class ViralsController < ApplicationController
   def show
      @viral = Viral.find params[:id]
   end
end

Because Ruby is object orientated , it expects each @instance_variable to be an object assigned by Ruby. 因为Ruby是面向对象的 ,所以它希望每个@instance_variable都是Ruby分配的对象

This means that if you pass a valid Ruby object to the link_to method, Rails will extract all the necessary data to invoke the show action for it... 这意味着,如果将有效的Ruby对象传递给link_to方法,Rails将提取所有必需的数据以为其调用show动作。

<%= link_to @viral.id, @viral %>

... will actually be ... ...实际上将是...

<%= link_to @viral.id, viral_path(@viral.id) %>

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

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