简体   繁体   中英

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 "Viral", @viral %>

I'm under the impression that the "Viral" is the label for the link, however i have no idea what @viral is. 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.

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.

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.

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

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

@viral is a variable (object)

link_to is a helper method what make html link

<%= 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 Text", "http://link-path.com" %>

The first argument is the anchor text, the second is the URL the browser will request.


Since Rails is an application framework, you can pass dynamic values to the link_to arguments. 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...

#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):

#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.

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...

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

... will actually be ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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