Rails - “无法将符号转换为字符串”仅限生产

发布于 2024-10-16 22:39:44 字数 839 浏览 2 评论 0原文

我有显示特定于型号的闪存消息的部分视图。部分如下所示:

app/views/mymodel/_flashpartial.erb

<% flash.each do |key, value| %>
  <% if model_key = myModelFlash(key) then %>
    <%= content_tag(:div, value, :class => "flash #{model_key}") %>
  <% end %>
<% end %>

myModelFlash 方法仅获取密钥并使用简单的正则表达式检查特定前缀。它位于

app/helpers/mymodelhelper.rb

module MyModelHelper
  def myModelFlash( key )
    m = /^_mymodel_(.*)/.match(key)
    m[1] unless m.nil?
  end
end

中,这在我的开发和测试环境中运行得非常好。一旦进入 Heroku,我就会收到一条错误消息 (ActionView::Template::Error)“无法将符号转换为字符串”,指向对 match 的调用。

如果我从视图中删除对 myModelFlash 的调用并仅显示键和值,那么就不会出错,因此至少键和值会进入部分景色就好了。由于某种原因,辅助方法认为传递给它的键是一个符号而不是一个字符串。

关于为什么会发生这种情况有什么想法吗?

I have partial view that displays model-specific flash messages. The partial looks like:

app/views/mymodel/_flashpartial.erb

<% flash.each do |key, value| %>
  <% if model_key = myModelFlash(key) then %>
    <%= content_tag(:div, value, :class => "flash #{model_key}") %>
  <% end %>
<% end %>

The myModelFlash method simply takes the key and checks for a particular prefix using a simple regex. It's located in

app/helpers/mymodelhelper.rb

module MyModelHelper
  def myModelFlash( key )
    m = /^_mymodel_(.*)/.match(key)
    m[1] unless m.nil?
  end
end

This works perfectly fine in my development and test environments. As soon as it goes onto Heroku, I get an error saying (ActionView::Template::Error) "can't convert Symbol into String" pointing to the call to match.

If I remove the call to myModelFlash from the view and simply display the key and value, that works just fine in terms of not erroring out, so at the very least the key and value are getting into the partial view just fine. For some reason the helper method thinks that the key being passed into it is a symbol and not a String.

Any ideas as to why this might be happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-10-23 22:39:44

我建议您使用 key.to_s 作为快速解决方法。

您的问题的原因可能是测试服务器和生产服务器之间某些组件的某些版本不同。
如果你的测试通过了,而你的生产环境崩溃了,那是一个非常糟糕的情况。

您应该比较 ruby​​ 的版本和您正在使用的所有 gem。如果您使用“bundler”,那么“bundle list”会给出一个很好的摘要。

如果您发现所有版本都相同...那么,我们将寻找另一个原因。

更新

看来问题不是由版本差异引起的,而是由flash中的意外数据引起的,显然在生产环境中可能与测试中不同。

我建议您稍微更改一下 myModelFlash 方法。

def myModelFlash( key )
  if m = /^_mymodel_(.*)/.match(key.to_s)
    return m[1]
  end
end

闪存可能包含不同的键,其中一些可能是符号或其他任何东西,因此您必须准备好处理所有这些键。

key 参数转换为 .to_s 应该是一个安全的选择,但如果您确定始终设置闪存键(我的意思是与此“_mymodel”相关的键)问题)作为字符串,您可以更改此方法的第一行:

def myModelFlash( key )
  if key.is_a?(String) && m = /^_mymodel_(.*)/.match(key.to_s)
    return m[1]
  end
end

在您的测试中,向 Flash 添加一些其他键,然后测试操作如何处理它们。

I suggest you just use key.to_s as a quick workaround.

The reason for your problem may be that some version of some component differs between your testing server and the production server.
If your tests pass, and your production environment crashes, that is a very bad situation.

You should compare the versions of ruby and all of the gems you are using. If you use 'bundler' then 'bundle list' gives a nice summary.

If you find out that all the versions are the same... Well, we will be looking for another reason.

Update

As it seems that the problem is caused not by the version differences, but by unexpected data in flash, which obviously in production environment may be different than in testing.

I suggest you change the myModelFlash method a little.

def myModelFlash( key )
  if m = /^_mymodel_(.*)/.match(key.to_s)
    return m[1]
  end
end

The flash may contain different keys, some of them may be Symbols or really anything, so you must be prepared to handle all of them.

Converting the key parameter with .to_s should be a safe choice, but if you are sure that you always set the flash keys (I mean the keys related to this "_mymodel" issue) as Strings, you may change the first line of this method:

def myModelFlash( key )
  if key.is_a?(String) && m = /^_mymodel_(.*)/.match(key.to_s)
    return m[1]
  end
end

And in your test, add a few other keys to your flash, and then test how the action handles them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文