简体   繁体   English

如何产生 <p id=“r1_c1”> 用红宝石插值?

[英]How to generate <p id=“r1_c1”> with ruby interpolation?

I have a need to generate an html id using some .erb code. 我需要使用一些.erb代码生成html id。 Specifically, I want to use the row and column index of a cell in a rectangular array (made out of a table of cells). 具体来说,我想使用矩形阵列(由单元格表制成)中单元格的行和列索引。 I have a method cell.web_id, which constructs a string thus: 我有一个方法cell.web_id,它因此构造了一个字符串:

"r#{row_index}_c#{column_index}", 

so that cell(2,3) would yield 这样cell(2,3)将产生

"r2_c3".  

So far, so good. 到现在为止还挺好。

Now for the odd part. 现在是奇怪的部分。 I haven't figured out how to wrap the web_id result in double quotes required to properly define the attribute. 我还没有弄清楚如何将web_id结果包装在正确定义属性所需的双引号中。 No matter what I try, I always get: 无论我尝试什么,我都会得到:

<span class="cell" id=&quot;r1_c1&quot; >

This form doesn't work. 此表格无效。

So, how can I get this form: 因此,如何获得此表格:

<span class="cell" id="r2_c3" >

I know this is easy if you know the proper incantation, but I don't have it. 如果您知道正确的咒语,我知道这很容易,但是我没有。

Try this: 尝试这个:

<span class="cell" id="<%="r#{row_index}_c#{column_index}"%>" >

OR 要么

<span class="cell" id="r<%=row_index%>_c<%=column_index%>" >

OR 要么

<span class="cell" id="<%=foo_span_id(row_index, column_index)%>" >

Where foo_span_id is a custom helper method 其中foo_span_id是自定义帮助程序方法

"\\"r#{row_index}_c#{column_index}\\"".html_safe

(apidock文档) (通常无用,但出于完整性考虑而包括在内)

Instead double quotes, why don't you try single quotes? 而是使用双引号,为什么不尝试使用单引号呢? (that is normally what I do in situations like this). (通常是我在这种情况下所做的事情)。

So your code will be 所以你的代码将是

"'r#{row_index}_c#{column_index}'"

and your HTML will also need to be slightly changed to single quotes 并且您的HTML还需要稍微更改为单引号

<span class='cell' id='r2_c3'; >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM