to_param和Gem: friendly_id构造合适的url

!!!!!!!to_params:
to_param()Link
Returns a string representing the object’s key suitable for use in URLs, or nil if persisted? is false.

class Person < ActiveRecord::Base
end

person = Person.create
person.to_param # => “1”
to_param()Link
Returns a String, which Action Pack uses for constructing an URL to this object. The default implementation returns this record’s id as a String, or nil if this record’s unsaved.

For example, suppose that you have a User model, and that you have a resources :users route. Normally, user_path will construct a path with the user object’s ‘id’ in it:

user = User.find_by(name: ‘Phusion’)
user_path(user) # => “/users/1”
You can override to_param in your model to make user_path construct a path using the user’s name instead of the user’s id:

class User < ActiveRecord::Base
def to_param # overridden
name
end
end

user = User.find_by(name: ‘Phusion’)
user_path(user) # => “/users/Phusion”
to_param(namespace = nil)Link
Returns a string representation of the receiver suitable for use as a URL query string:

{name: ‘David’, nationality: ‘Danish’}.to_param

=> “name=David&nationality=Danish”

An optional namespace can be passed to enclose the param names:

{name: ‘David’, nationality: ‘Danish’}.to_param(‘user’)

=> “user[name]=David&user[nationality]=Danish”

The string pairs “key=value” that conform the query string are sorted lexicographically in ascending order.

This method is also aliased as to_query.

Also aliased as: to_query
Source: show | on GitHub

!!!!!!!!!!!friendly_id:
Setting Up FriendlyId in Your Model
To use FriendlyId in your ActiveRecord models, you must first either extend or include the FriendlyId module (it makes no difference), then invoke thefriendly_id method to configure your desired options:
class Foo < ActiveRecord::Base
include FriendlyId
friendly_id :bar, :use => [:slugged, :simple_i18n]
end
The most important option is :use, which you use to tell FriendlyId which addons it should use. See the documentation for FriendlyId::Base#friendly_id for a list of all available addons, or skim through the rest of the docs to get a high-level overview.
A note about single table inheritance (STI): you must extend FriendlyId in all classes that participate in STI, both your parent classes and their children.
The Default Setup: Simple Models

The simplest way to use FriendlyId is with a model that has a uniquely indexed column with no spaces or special characters, and that is seldom or never updated. The most common example of this is a user name:
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :login
validates_format_of :login, :with => /\A[a-z0-9]+\z/i
end

@user = User.friendly.find “joe” # the old User.find(1) still works, too
@user.to_param # returns “joe”
redirect_to @user # the URL will be /users/joe
In this case, FriendlyId assumes you want to use the column as-is; it will never modify the value of the column, and your application should ensure that the value is unique and admissible in a URL:
class City < ActiveRecord::Base
extend FriendlyId
friendly_id :name
end

@city.friendly.find “Viña del Mar”
redirect_to @city # the URL will be /cities/Viña%20del%20Mar
Writing the code to process an arbitrary string into a good identifier for use in a URL can be repetitive and surprisingly tricky, so for this reason it’s often better and easier to use slugs.
Performing Finds with FriendlyId
FriendlyId offers enhanced finders which will search for your record by friendly id, and fall back to the numeric id if necessary. This makes it easy to add FriendlyId to an existing application with minimal code modification.
By default, these methods are available only on the friendly scope:
Restaurant.friendly.find(‘plaza-diner’) #=> works
Restaurant.friendly.find(23) #=> also works
Restaurant.find(23) #=> still works
Restaurant.find(‘plaza-diner’) #=> will not work
Restoring FriendlyId 4.0-style finders

Prior to version 5.0, FriendlyId overrode the default finder methods to perform friendly finds all the time. This required modifying parts of Rails that did not have a public API, which was harder to maintain and at times caused compatiblity problems. In 5.0 we decided to change the library’s defaults and add the friendly finder methods only to the friendly scope in order to boost compatiblity. However, you can still opt-in to original functionality very easily by using the :finders addon:
class Restaurant < ActiveRecord::Base
extend FriendlyId

scope :active, -> {where(:active => true)}

friendly_id :name, :use => [:slugged, :finders]
end

Restaurant.friendly.find(‘plaza-diner’) #=> works
Restaurant.find(‘plaza-diner’) #=> now also works
Restaurant.active.find(‘plaza-diner’) #=> now also works
Updating your application to use FriendlyId’s finders

Unless you’ve chosen to use the :finders addon, be sure to modify the finders in your controllers to use the friendly scope. For example:

before

def set_restaurant
@restaurant = Restaurant.find(params[:id])
end

after

def set_restaurant
@restaurant = Restaurant.friendly.find(params[:id])
end
Active Admin

Unless you use the :finders addon, you should modify your admin controllers for models that use FriendlyId with something similar to the following:
controller do
def find_resource
scoped_collection.friendly.find(params[:id])
end
end
Slugged Models
FriendlyId can use a separate column to store slugs for models which require some text processing.
For example, blog applications typically use a post title to provide the basis of a search engine friendly URL. Such identifiers typically lack uppercase characters, use ASCII to approximate UTF-8 characters, and strip out other characters which may make them aesthetically unappealing or error-prone when used in a URL.
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, :use => :slugged
end

@post = Post.create(:title => “This is the first post!”)
@post.friendly_id # returns “this-is-the-first-post”
redirect_to @post # the URL will be /posts/this-is-the-first-post
Candidates

Since UUIDs are ugly, FriendlyId provides a “slug candidates” functionality to let you specify alternate slugs to use in the event the one you want to use is already taken. For example:
class Restaurant < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged

# Try building a slug based on the following fields in
# increasing order of specificity.
def slug_candidates
[
:name,
[:name, :city],
[:name, :street, :city],
[:name, :street_number, :street, :city]
]
end
end

r1 = Restaurant.create! name: ‘Plaza Diner’, city: ‘New Paltz’
r2 = Restaurant.create! name: ‘Plaza Diner’, city: ‘Kingston’

r1.friendly_id #=> ‘plaza-diner’
r2.friendly_id #=> ‘plaza-diner-kingston’
To use candidates, make your FriendlyId base method return an array. The method need not be named slug_candidates; it can be anything you want. The array may contain any combination of symbols, strings, procs or lambdas and will be evaluated lazily and in order. If you include symbols, FriendlyId will invoke a method on your model class with the same name. Strings will be interpreted literally. Procs and lambdas will be called and their return values used as the basis of the friendly id. If none of the candidates can generate a unique slug, then FriendlyId will append a UUID to the first candidate as a last resort.

最后这个数组方式意思是friendly_id生成唯一的字符串形式, 例r1.friendly_id #=> ‘plaza-diner’, 当r2.friendly_id #=> ‘plaza-diner-kingston’也就是r 2和r1的第一个元素相同生成的字符串相同的时候, 则r2自动匹配数组第二个元素和第一个元素相加的结果,因为friendly_id总是生成唯一的字符串形式, 所以r1.friendly_id返回 ‘plaza-diner’数组第一个元素的结构, r2.friendly_id返回’plaza-diner-kingston’数组第一个元素加第二个元素的结构.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值