开发者

How to concatenate integer to string in ERB?

开发者 https://www.devze.com 2023-03-18 08:54 出处:网络
If item_counter=213 then I want to set item_id to \"item213\". S开发者_开发技巧eems easy but: <% item_id = \"item\" + item_counter %>

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}" %>
0

精彩评论

暂无评论...
验证码 换一张
取 消