简体   繁体   English

在Rails中,如何在ruby_to的字符串中添加ruby语法?

[英]In rails, how to add ruby syntax to a string in link_to?

I have the following link_to 我有以下link_to

= link_to 'Get Driving Directions!', 'http://maps.google.com/maps?saddr=starting&daddr=ending&hl=en'

I want to replace the starting and ending with ruby syntax, location.start and location.end 我想用ruby语法, location.startlocation.end替换开始结尾

I've tried 我试过了

= link_to 'Get Driving Directions!', 'http://maps.google.com/maps?saddr=' + location.start+ '&daddr=' + location.end + '&hl=en'

but that does not seem to work. 但这似乎不起作用。 What is the best way to do this? 做这个的最好方式是什么?

Edit: After playing around, it appears that although location.start and location.end are strings in the database, they are not when I try to add them on to other strings. 编辑:玩了之后,似乎虽然location.startlocation.end是数据库中的字符串,但是当我尝试将它们添加到其他字符串时它们不是。 In order to do so, Im must explicitly specify .to_string to both location.start and location.end 为此,Im必须为location.startlocation.end都明确指定.to_string。

However, when I do this, the strings no longer appears on the show page. 但是,当我这样做时,字符串不再出现在显示页面上。 What is going on here? 这里发生了什么?

If you are getting "no implicit conversion of nil into String" error, it means that your location.start or location.end is nil. 如果收到“没有将nil隐式转换为String的错误”错误,则意味着您的location.start或location.end为nil。 So you should add a conditional check: 因此,您应该添加条件检查:

- if location.start.present? & location.end.present?
   = link_to 'Get Driving Directions!', 'http://maps.google.com/maps?saddr=' + location.start+ '&daddr=' + location.end + '&hl=en'
- elsif !location.start.present? & location.end.present?
   = link_to 'Get Driving Directions!', 'http://maps.google.com/maps?saddr=defaultstart' + '&daddr=' + location.end + '&hl=en'
- else
   = link_to 'Get Driving Directions!', 'http://maps.google.com/maps?saddr=defaultstart&daddr=defaultend&hl=en'

Where defaultstart and defaultend can be replaced by default values. 可以将defaultstart和defaultend替换为默认值。

You can use string interpolation: 您可以使用字符串插值:

= link_to 'Get Driving Directions!', "http://maps.google.com/maps?saddr=#{location.start}&daddr=#{location.end}&hl=en"

Note that I've used " (quotes) instead of ' (single quotes) when using string interpolation otherwise it won't work. 请注意,在使用字符串插值时,我使用了" (引号)而不是' (单引号),否则将无法使用。

You can use to_params method. 您可以使用to_params方法。

params = {"saddr" => "starting", "daddr" => "ending", "hl" => "en"}.to_param
=> "daddr=ending&hl=en&saddr=starting"

url = 'http://maps.google.com/maps?' + params
=> "http://maps.google.com/maps?daddr=ending&hl=en&saddr=starting"

or use uri . 或使用uri

require 'uri'
uri = URI.parse('http://maps.google.com/maps')
uri.query = URI.encode_www_form("saddr" => "starting", "daddr" => "ending", "hl" => "en")
#uri.query = URI.encode_www_form("saddr" => "#{location.start}", "daddr" => "#{location.end}", "hl" => "en")
=> "saddr=starting&daddr=ending&hl=en"

uri.to_s
=> "http://maps.google.com/maps?saddr=starting&daddr=ending&hl=en"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM