Heroku Ruby NoMethodError string.capitalize
我使用以下代码根据第一个字母对位置进行分组。
mobile_controller:
def index
@locations = Location.all.group_by{|l| l.name[0].capitalize.match(/[A-Z]/) ? l.name[0].capitalize : "#"}
end
视图:
<% @locations.keys.sort.each do |starting_letter| %>
<%= starting_letter %>
<% @locations[starting_letter].each do |location| %>
<%= location.name %>
<% end %>
<% end %>
在我的本地计算机上一切正常,但heroku不喜欢它并不断向我显示此错误:
NoMethodError (undefined method `capitalize' for 66:Fixnum):
app/controllers/mobile_controller.rb:13:in `search'
app/controllers/mobile_controller.rb:13:in `search'
如何解决此问题?
提前致谢
解决方案: 将我的 Heroku Stack 更新为 Ruby 1.9。
I use the following code to group locations depending on the first letter.
mobile_controller:
def index
@locations = Location.all.group_by{|l| l.name[0].capitalize.match(/[A-Z]/) ? l.name[0].capitalize : "#"}
end
view:
<% @locations.keys.sort.each do |starting_letter| %>
<%= starting_letter %>
<% @locations[starting_letter].each do |location| %>
<%= location.name %>
<% end %>
<% end %>
Everything works fine on my local machine, but heroku doesn't like it and keeps showing me this error:
NoMethodError (undefined method `capitalize' for 66:Fixnum):
app/controllers/mobile_controller.rb:13:in `search'
app/controllers/mobile_controller.rb:13:in `search'
How can I fix this?
Thanks in advance
Solution:
Updated my Heroku Stack to Ruby 1.9.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的本地计算机可能运行在 Ruby 1.9 上,而您的 Heroku 应用程序运行在 1.8 上。
在 Ruby 1.8 中,调用
String#[]
将为您提供字符代码(数字),而 Ruby 1.9 将为您提供带有第一个字符的字符串。您可以使用
l.name[0..0]
来解决这个问题,或者切换到 Heroku 上的 Ruby 1.9 堆栈。Your local machine is probably on Ruby 1.9, and your Heroku app is running on 1.8.
In Ruby 1.8, calling
String#[]
will give you the character code (a number), whereas Ruby 1.9 will give you a string with the first character.You can use
l.name[0..0]
to get around this, or switch to a Ruby 1.9 stack on Heroku.在 Ruby 1.8 下,
String#[]
返回引用字符的 ASCII 代码而不是字符本身。在控制器中尝试l.name[0,1].capitalize
。Under Ruby 1.8,
String#[]
returns the ASCII code of the referenced character rather than the character itself. Tryl.name[0,1].capitalize
in your controller.