How to concatenate integer to string in ERB?
If item_counter=213 then I want to set item_id to "item213". S开发者_开发技巧eems easy but:
<% item_id = "item" + item_counter %>
results in an error: can't convert Fixnum into String
<% item_id = "item" + item_counter.chr %>
outputs a strange character: item
<% item_id = "item#item_counter" %>
is understood as item#item_counter
What is the correct way to concatenate an integer to a string in ERB (Ruby on rails 3)?
to_s
is the method you're looking for:
<% item_id = "item" + item_counter.to_s %>
You can also use string interpolation:
<% item_id = "item#{item_counter}" %>
精彩评论