rails 对 URL的一些处理

u = "http://www.youtube.com/watch?v=xxxxxxxxxxxxxxxxxxx"
u1 = URI.parse('www.google.com')
u1.query = URI.parse(u).query
u1.to_s

有个转换查询串的方法,好像是叫 Rack::Utils.parse_query ,转成哈希。哈希的to_query 又能转换成查询串。

查询串的字段之间一般用&符号分隔,但也可以是别的,好像是半角分号也可以。所以自己转换的话,可能出现不兼容的情况

set_query 方法

For Rails 2: You want request.url(全路径) instead of request.request_uri.(相对路径) This combines the protocol (usually http://) with the host, and request_uri to give you the full address.

For Rails 3: You want "#{request.protocol}(协议)#{request.host_with_port}(主机地址端口)#{request.fullpath}(相对地址)", since request.url is now deprecated(废弃).

url_for(:only_path => false)
url_for(params.merge(:tag => "lol"))

current_url(:page=>4) --> http://...&page=4

root_url(:only_path => false)

<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>

你可能感兴趣的:(Rails)