Rails 两层引号的转义字符

Rails escape characters for two layers of quotations

我正在尝试将自己的百分比放入 Bootstrap 进度条中。浮点数可以很好地传递给视图,但由于某种原因 Bootstrap 进度条不想读取它。我首先猜测我没有正确转义 style 标签。

观点:

<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="#{ <% @app.completion_status %> }" aria-valuemin="0" aria-valuemax="100" style="width: '#{raw(<%= number_to_percentage(@app.completion_status) %>)}';">
    <%= @app.completion_status %>%
  </div>
</div>

细看:

style="width: '#{raw(<%= number_to_percentage(@app.completion_status) %>)}';"

The value attempting to be passed and converted to a percentage is: `33.3333333333333` (one third)

控制器:

def set_completion
  @app = App.find(params[:id])

  @app.update_attribute(:completion_status,

  (((@app.elements.where(completion: true).count) / (@app.elements.count).to_f) * 100 )

  )
end

您是否尝试过切换 '"

<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="#{ <% @app.completion_status %> }" aria-valuemin="0" aria-valuemax="100" style='width: "#{raw(<%= number_to_percentage(@app.completion_status) %>)}";'>
    <%= @app.completion_status %>%
  </div>
</div>

乍一看,我发现您在 erb 标签之外使用 ruby 插值,这可能是问题所在。编辑::试试下面

style="width: <%= number_to_percentage(@app.completion_status) %>"

下面是我刚刚构建的一个,所以请尝试遵循这一点并将您的插值保留在 ERB 标签内!

<div class="progress-bar progress-bar-info progress-bar-striped"
 role="progressbar" aria-valuenow="<%=@email_usage%>" aria-valuemin="0"
 aria-valuemax="100" style="width: <%=@email_usage%>%">