Domanda

I want to do something like this:

in controller:

res=1+3
if ...
flash[:notice]="the result is" + res
end

However, I found that it does not work, and the error message is like this:

no implicit conversion of Fixnum into String
È stato utile?

Soluzione

You need to convert it to the res variable which is an integer to a string:

flash[:notice]="the result is" + res.to_s

Altri suggerimenti

I would do it using string interpolation:

flash[:notice] = "the result is #{res}"

Which is better than concatenation (using + ) because:

  1. Its faster
  2. As you can see it automatically do .to_s for you
  3. Less typing (developers are lazy ofcourse)

See this SO question for more details and comparison

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top