0

哈!我的模板格式奇怪,我无法理解如何/为什么。

<% @poll.questions_all.each do |q| %>
          <td>
            <span class="optionvalue">
              <% if can? :read_full, @poll %>
                <%= resp[:texts][q.id] %>
              <% else %>
                <%= resp[:texts][q.id].nil? ? '' : resp[:texts][q.id].gsub(Question::POISON_WORDS_REGEX, '---') %>
              <% end %>
            </span>
            <% unless q.options.empty? %>
              <%= q.get_matching_option(resp[:texts][q.id])? ": #{q.get_matching_option(resp[:texts][q.id])}" : '' %>
            <% end %>
          </td>
        <% end %>

结果如下:

A : Playgrounds

不知道第一个空间是从哪里形成的,所有摆脱它的努力都失败了!生成的标记:

<td>
   <span class="optionvalue">
       A
   </span>
       : Playgrounds
</td>
4

3 回答 3

1

尝试使用<%- -%>erb 标签的版本,这些应该抑制额外的空格:

<%- if can? :read_full, @poll -%>
  <%= resp[:texts][q.id] %>
<%- else -%>
  <%= resp[:texts][q.id].nil? ? '' : resp[:texts][q.id].gsub(Question::POISON_WORDS_REGEX, '---') %>
<%- end -%>

我不记得你是否在开始和结束标签中都需要它们,但我建议你试试看你是否能得到你想要的。

于 2012-10-11T02:42:51.810 回答
0

可怕的解决方案,但它的工作原理:

            <% @poll.questions_all.each do |q| %>
          <td>
            <span class="optionvalue">
              <% if can? :read_full, @poll %>
                <%= resp[:texts][q.id] %></span><%= q.get_matching_option(resp[:texts][q.id])? ": #{q.get_matching_option(resp[:texts][q.id])}" : '' unless q.options.empty? %>
              <% else %>
                <%= resp[:texts][q.id].nil? ? '' : resp[:texts][q.id].gsub(Question::POISON_WORDS_REGEX, '---') %></span><%= q.get_matching_option(resp[:texts][q.id])? ": #{q.get_matching_option(resp[:texts][q.id])}" : '' unless q.options.empty? %>
              <% end %>
          </td>
        <% end %>
于 2012-10-11T02:00:45.830 回答
0

erb如果您将所有内容都保留在一行 HTML 中,我很确定会做正确的事情(而不是添加不需要的行)。

对于这种情况,一般来说,我建议您将用于生成内容的任何逻辑放在视图帮助文件中的方法中(或者如果您觉得懒惰,请在 erb 的代码块中,如本例所示):

<% @poll.questions_all.each do |q| %>
  <% 
    if can? :read_full, @poll
      texts = resp[:texts][q.id]
    else
      texts = (resp[:texts][q.id].nil? ? '' : resp[:texts][q.id].gsub(Question::POISON_WORDS_REGEX, '---'))
    end
    if q.options.empty?
      matching_options = ''
    else 
      matching_options = (q.get_matching_option(resp[:texts][q.id])? ": #{q.get_matching_option(resp[:texts][q.id])}" : '')
    end
  %> 
  <td>
    <span class="optionvalue"><%= texts %></span><%= matching_options %>
  </td>
<% end %>
于 2012-10-11T02:22:44.670 回答