Rails 2.3.8 - 当框包含多个选项时,选择框会导致解析错误

发布于 2024-10-22 05:40:00 字数 1530 浏览 4 评论 0原文

该应用程序利用 jQuery 执行 ajax 请求,以便填充相关选择框。我的控制器操作 responds_to :js,在 .js.erb 文件中,我有:

str += '<%= f.select field.name, list, {}, { :class => "list", :multiple => "multiple", :style => "size:8; width:100px;" } %>';

“list”的填充如下:

str += '<% list = @validation_model.lookup([field], @lookup) %>';

我将其 .append()'ing 到一个 div 中。在 .js.erb 模板文件的末尾。但是,当“list”包含多个值时,模板的解析会失败。如果为空,或者只包含一个值,则解析成功。

这是我遇到的错误,还是我做错了什么?请注意,这显示为解析错误而不是运行时错误,因此我无法准确确定 Firebug 或 Safari Dev 中的问题所在。

更新:这是 .js.erb 文件的完整代码。我已用内联逻辑替换“列表”变量来检索数组。

str = '<% fields_for :mapapps do |f| %>';
<% for tf in @tag.tag_fields.find(:all, :order => :sequence) %>
    <% field = tf.parentfield %>
    <% if [email protected]?(field) %>
        $("#<%= field.name %>").remove();
    <% else %>
        <% next %>
    <% end %>
    str += '<div id="<%= field.name %>" class="floater">';
    str += '<label for="mapapps_<%= field.name %>"><%= field.label %></label>';
    str += '<%= f.select field.name, @validation_model.lookup([field], @lookup), {}, { :class => "list", :multiple => "multiple", :style => "size:8; width:100px;" } %>';
    str += '</div>';
<% end %>
str += '<% end %>'
$("#mfrsdiv").append(str);

The app in question utilizes jQuery to do ajax requests in order to populate dependent select boxes. My controller action responds_to :js, and in the .js.erb file I have:

str += '<%= f.select field.name, list, {}, { :class => "list", :multiple => "multiple", :style => "size:8; width:100px;" } %>';

"list" is populated like so:

str += '<% list = @validation_model.lookup([field], @lookup) %>';

I am .append()'ing this to a div. at the end of the .js.erb template file. However, when "list" contains more than one value, the parsing of the template fails. If it is empty, or contains only one value, parsing is successful.

Is this a bug I am running into, or am I doing something wrong? Please note this is appearing as a parse error rather than a runtime error, so I have been unable to determine exactly what the problem is in either Firebug or Safari Dev.

UPDATE: Here is the full code of the .js.erb file. I have replaced the "list" variable with inline logic to retrieve an array.

str = '<% fields_for :mapapps do |f| %>';
<% for tf in @tag.tag_fields.find(:all, :order => :sequence) %>
    <% field = tf.parentfield %>
    <% if [email protected]?(field) %>
        $("#<%= field.name %>").remove();
    <% else %>
        <% next %>
    <% end %>
    str += '<div id="<%= field.name %>" class="floater">';
    str += '<label for="mapapps_<%= field.name %>"><%= field.label %></label>';
    str += '<%= f.select field.name, @validation_model.lookup([field], @lookup), {}, { :class => "list", :multiple => "multiple", :style => "size:8; width:100px;" } %>';
    str += '</div>';
<% end %>
str += '<% end %>'
$("#mfrsdiv").append(str);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-10-29 05:40:00

我认为您的印象是 eruby 项目可以附加到字符串并在视图中计算。实际上,eruby 项目是在 .js.erb 文件本身中计算的,然后附加到字符串中。在您的代码中,f 必须是视图中的表单变量。它在 .js.erb 文件中没有值。如果您确实想将此选择添加到表单中,您应该考虑部分选择并使用 Ajax。

I think you are under the impression that eruby items can be appended to the string and will be calculated in the view. Actually the eruby items are calculated in .js.erb file itself and then appended to the string. In your code, f must be the form variable from the view..it will not have value in the .js.erb file. If you really want to add this select to the form, you should be thinking of a partial and using Ajax.

转瞬即逝 2024-10-29 05:40:00

我让代码工作了。我所需要做的就是在对 f.select 的调用周围添加“escape_javascript”。

str += '<%= escape_javascript(f.select field.name, @validation_model.lookup([field], @lookup), {}, { :class => "list", :multiple => "multiple", :style => "size:8; width:100px;" }) %>';

应该注意的是,我上面问题中的代码仍然会因为 ruby​​prince 提到的原因而失败,但渲染问题本身是由 escape_javascript 解决的。

I got the code working. All I needed to do was to put "escape_javascript" around the call to f.select.

str += '<%= escape_javascript(f.select field.name, @validation_model.lookup([field], @lookup), {}, { :class => "list", :multiple => "multiple", :style => "size:8; width:100px;" }) %>';

It should be noted that the code in my question above will still fail for the reasons mentioned rubyprince, but the rendering issue itself is solved by the escape_javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文