美文网首页
2. Routing - 2.4 Named Routes

2. Routing - 2.4 Named Routes

作者: 介可能是只乌龟 | 来源:发表于2016-03-28 08:39 被阅读0次

2.4 Named Routes

The idea of naming a route is basically to make like easier on you. There are no outwardly visible effects as far as the application is concerned.

When you name a route, a new method gets defined for use in your controllers and views; the method is called name_url (with name being the name you gave the route), and calling the method, with appropriate arguments, results in a URL being generated for the route. In addition, a method called name_path also gets created; this method generates just the path part of the URL, without the protocol and host components.

2.4.1 Creating a Named Route

get 'help' => 'help#index', as: 'help'

# link_to "Help", help_path

You can test named routes in the console directly using the special app object.

>> app.clients_path
=> "/clients"
>> app.clients_url
=> "http://www.example.com/clients"

2.4.2 name_path vs. name_url

When you create a named route, you’re actually creating at least two route helper methods. In the preceding example, those two methods are help_url and help_path. The difference is that the _url method generates anentire URL, including protocol and domain, whereas the _path method generates just the path part (sometimes referred to as an absolute path or a relative URL).

2.4.3 What to Name Your Routes

the best way to figure out what names you should use for your routes is to follow REST conventions, which are baked into Rails and simplify things greatly.

link_to"Auctionof#{item.name}",
  controller: "items",
  action: "show",
  id: item.id

# the routing rule to match that path
get "item/:id" => "item#show"

# good cadidate for named route
get "item/:id" => "item#show", as: "item"

# lets improve the situation by introducing item_path int the call to link_to

link_to "auction of #{item.name}", item_path(id: item.id)

2.4.4 Argument Sugar

If you need to supply an id number as an argument to a named route, you can just supply the number, without spelling out the :id key:

link_to "Auction of #{item.name", item_path(item.id)

link_to "Auction of #{item.name", item_path(item)

# multipul segments keys in path
get "auction/:auction_id/item/:id" => "item#show", as: "item"

link_to "Auction of #{item.name", item_path(auction, item)

Here, we’re letting Rails infer the ids of both an auction object and an item object, which it does by calling to_param on whatever non-hash arguments you pass into named route helpers. As long as you provide the arguments in the order in which their ids occur in the route’s pattern string, the correct values will be dropped into place in the generated path.

2.4.5 A Little More Sugar with Your Sugar

Furthermore, it doesn’t have to be the id value that the route generator inserts into the URL. As alluded to a moment ago, you can override that value by defining a to_param method in your model.

def to_param
  description.parameterize
end

#Subsequently, the method call `item_path(auction, item)` will produce something like:
/auction/3/item/cello-bow

Also, it provides a “munged” (stripped ofpunctuation and joined with hyphens) version of the description, courtesy of the parameterize method addedto strings in Active Support. It's possible to do something like:

Item.where(munged_description:params[:id]).first!

相关文章

网友评论

      本文标题:2. Routing - 2.4 Named Routes

      本文链接:https://www.haomeiwen.com/subject/ehjdlttx.html