Rails - "cannot convert character to string" for ONLY

I have a partial view that displays model specific flash messages. Partially looks like this:

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 specific prefix with a simple regular expression. It is located in

application / helpers / mymodelhelper.rb

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

This works great in my development and testing environment. As soon as it switches to Heroku, I get the error message (ActionView :: Template :: Error) "cannot convert character to string", indicating a match call.

If I remove the call from myModelFlash from the view and just show the key and value, this works just fine in terms of no errors, so at least the key and value get into a partial view just fine. For some reason, the helper method considers that the key passed to it is a character, not a string.

Any ideas as to why this could be happening?

+4
source share
1 answer

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

The cause of your problem may be that some version of a component is different between your test server and the production server. If your tests pass and your production environment fails, this is a very bad situation.

You should compare the versions of ruby ​​and all the gems you use. If you are using a "bundler" then a "list of packages" gives a good summary.

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

Update

It seems that the problem is not caused by version differences, but by unexpected data in flash memory, which, obviously, in a production environment may differ from testing.

I suggest changing the myModelFlash method a myModelFlash .

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

The flash may contain different keys, some of them can be characters or really any, so you should be prepared to handle all of them.

Converting the key parameter using .to_s should be a safe choice, but if you are sure to always set the flash keys (I mean the keys related to this _mymodel problem) as strings, you can 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 more keys to your flash, and then check how the action processes them.

+3
source

Source: https://habr.com/ru/post/1339058/


All Articles