Ruby-on-rails Rails 在视图中连接字符串和变量的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13558255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails best way to concatenate a string with a variable in a view
提问by Catfish
Essentially I'm trying to perform this comparison
基本上我正在尝试执行此比较
request.fullpath == "/contacts/"+current_user.id
What is the best way to concatenate a string with a variable to perform a comparison like this?
将字符串与变量连接以执行这样的比较的最佳方法是什么?
回答by joofsh
You can interpolate it as request.fullpath == "/contacts/#{current_user.id}"
您可以将其插入为 request.fullpath == "/contacts/#{current_user.id}"
回答by doesterr
"/contacts/#{current_user.id}"
Always use String interpolation, it's faster than <<and +
始终使用字符串插值,它比<<和更快+

