[英]String formatting in Ruby on Rails?
I have fields for street name, city, state and zipcode for User model, and I want to simply make a Google Map query with these values like 我有用户模型的街道名称,城市,州和邮政编码字段,我想简单地使用以下值进行Google Map查询,例如
https://www.google.com/maps?q=your+query
so that I can open hyperlink it to the actual google map page. 这样我就可以打开超链接到实际的Google地图页面。 Now I am wondering how I can make these parameters into the format of your+query.
现在,我想知道如何将这些参数设置为您的+查询格式。 I want to know if I can do it in the template or controller and how I can pass those variables.
我想知道是否可以在模板或控制器中进行操作,以及如何传递这些变量。
Ruby includes the URI module which is the preferred way of manipulating URIs as it's aware of encoding: Ruby包括URI模块,这是处理URI的首选方式,因为它知道编码:
require 'uri'
uri = URI.parse('https://www.google.com/maps?q=your+query')
uri.query # => "q=your+query"
params = URI::decode_www_form(uri.query).to_h # => {"q"=>"your query"}
params['q'] = 'my query'
uri.query = URI::encode_www_form(params)
uri.to_s # => "https://www.google.com/maps?q=my+query"
Rails has a number of helpers for parsing and manipulating them though as that's what Rails is built around, so dig into your tutorial or book and take advantage of those. 尽管Rails就是基于Rails构建的,但Rails拥有许多用于解析和操纵它们的帮助程序,因此请深入研究您的教程或书籍并利用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.