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(method_name = nil) Link
Defines your model’s to_param method to generate “pretty” URLs using method_name, which can be any attribute or method that responds to to_s.

class User < ActiveRecord::Base
to_param :name
end

user = User.find_by(name: ‘Fancy Pants’)
user.id # => 123
user_path(user) # => “/users/123-fancy-pants”
Values longer than 20 characters will be truncated. The value is truncated word by word.

user = User.find_by(name: ‘David HeinemeierHansson’)
user.id # => 125
user_path(user) # => “/users/125-david”
Because the generated param begins with the record’s id, it is suitable for passing to find. In a controller, for example:

params[:id] # => “123-fancy-pants”
User.find(params[:id]).id # => 123

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

parameterize(sep = ‘-‘) Link
Replaces special characters in a string so that it may be used as part of a ‘pretty’ URL.

class Person
def to_param
“#{id}-#{name.parameterize}”
end
end

@person = Person.find(1)

=> #

=> Donald E. Knuth

使用用户友好的网址。在网址显示具描述性的模型属性,而不只是 id 。
有不止一种方法可以达成:

覆写模型的 to_param 方法。这是 Rails 用来给对象建构网址的方法。缺省的实作会以字串形式返回该 id 的记录。它可被另一个具人类可读的属性覆写。

class Person
def to_param
“#{id} #{name}”.parameterize
end
end
为了要转换成对网址友好 (URL-friendly)的数值,字串应当调用 parameterize 。 对象的 id 要放在开头,以便给 ActiveRecord 的 find 方法查找。

to_param()函数和parameterize()函数相关推荐

  1. render函数和redirect函数的区别+反向解析

    render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象)   request对象:----->所有的请求 ...

  2. Python day10 global关键字、函数递归、匿名函数、map函数的用法详解

    1.global关键字 引用全局变量,在局部全局变量改变,也会改变,global相当于指针,将地址指向全局变量的name name='littlepage'def littepage():global ...

  3. C++ 笔记(13)— 函数(函数声明、函数定义、函数调用[传值、指针、引用]、函数参数默认值、函数重载)

    每个 C++ 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数. 1. 函数声明 函数声明告诉编译器函数的名称.返回类型和参数.函数声明包括以下几个部分: ret ...

  4. Go 学习笔记(16)— 函数(02)[函数签名、有名函数、匿名函数、调用匿名函数、匿名函数赋值给变量、匿名函数做回调函数]

    1. 函数签名 函数类型也叫做函数签名,可以使用 fmt.Printf("%T") 格式化参数打印函数类型. package mainimport "fmt"f ...

  5. Go 学习笔记(15)— 函数(01)[函数定义、函数特点、多值返回、实参形参、变长参数,函数作为参数调用]

    1. 函数定义 Go 语言最少有个 main() 函数.函数声明告诉了编译器函数的名称,返回类型和参数. func funcName(parameter_list)(result_list) {fun ...

  6. MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据

    1. 字符串函数 MySQL 的常用函数包括字符串函数.数值函数.日期时间函数.流程函数等. SELECT ascii("abc"),char(97),concat("h ...

  7. 经常可能会用到的【函数节流和函数防抖】记录下,做下区分

    今天突然被人问到,函数节流和函数防抖的区别是什么, 结果我脑子一热直接举了个滚动条的粟子说是优化高频率执行的手段,就记得自己是用setTimeout来实现的. 完了区别是什么??哪个是哪个都蒙B了 回 ...

  8. c语言随机数生成0 99函数,C语言生成随机数的函数、延时函数

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 下面C语言代码使用了生成随机数的函数.延时函数.请大家仔细观察其显示效果. 从以下代码,我们可以得出一个重要的结论:当上述两类函数被放入循环时,应作出一定 ...

  9. 在python中使用关键字define定义函数_python自定义函数def的应用详解

    这里是三岁,来和大家唠唠自定义函数,这一个神奇的东西,带大家白话玩转自定义函数 自定义函数,编程里面的精髓! def 自定义函数的必要函数:def 使用方法:def 函数名(参数1,参数2,参数-): ...

  10. php的匿名函数和闭包函数

    php的匿名函数和闭包函数 tags: 匿名函数 闭包函数 php闭包函数 php匿名函数 function use 引言:匿名函数和闭包函数都不是特别高深的知识,但是很多刚入门的朋友却总是很困惑,因 ...

最新文章

  1. 解剖人脸识别从无到有的发展史
  2. Kubernetes 稳定性保障手册:洞察+预案
  3. 京东WebService调用 求助~~~~~
  4. linux7.2配置多路径软件,RHEL6使用系统自带多路径软件配置多路径,rhel6路径
  5. python手机编译器可以干什么_Python是什么?Python学习用哪些编译器?
  6. 如何通过任务调度实现百万规则报警
  7. ipv6正则表达式 java,用正则表达式解析IPv4跟IPv6地址字符串
  8. 一步步实现SDDC--学习平台环境的搭建
  9. linux下Makefile学习--注释很好
  10. OpenSea2月总交易额为9390.4万美元 用户总数突破5万人
  11. [个人原创]关于java中对象排序的一些探讨(一)
  12. Node.js之Stream可读流readable
  13. Java项目—在线考试系统
  14. SOTA级发丝抠图模型PP-Matting开源,支持多场景精细化分割
  15. 多图详解IT架构师完整知识体系及技术栈
  16. 什么是加密狗?加密狗由来
  17. stata 自相关专题【计量经济系列(五)】
  18. PHP开发环境搭建:PHP集成环境XAMPP 的安装与配置
  19. 基于Python语言豆瓣电影数据挖掘与分析
  20. redis集群管理-5.0.14版本

热门文章

  1. 亚信安全发现勒索软件新变种 Word文档成为导火索
  2. tomcat发布asp网站的解决办法(转)
  3. 近几年美国人口数据matlab,【美国人口2018总人数】美国人口数量2018|美国人口世界排名...
  4. linux软键盘怎么调出来,软键盘怎么关?软键盘关闭方法
  5. 三条中线分的六个三角形_为什么三角形的三条中线把三角形分为面积相等的六块...
  6. python3 Python.h No such file or directory
  7. 域名注册必须实名认证 《互联网域名管理办法》11月1日实施
  8. ffmpeg 图片合成视频
  9. 如何解决“Cannot be opened because the developer cannot be verified”
  10. 需求工程规格说明、需求验证、需求管理