Rails 4 - 带有dependent-fields-rails的条件JS [英] Rails 4 - Conditional JS with dependent-fields-rails

查看:83
本文介绍了Rails 4 - 带有dependent-fields-rails的条件JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何在我的Rails 4应用程序中使用dependent-fields-rails gem。

I'm trying to figure out how to use dependent-fields-rails gem with my Rails 4 app.

我迷失了。

我在我的供应商javascripts文件夹中包含了underscore.js并更新了我的application.js以包含(在底部,正上方需要树):

I have included underscore.js in my vendor javascripts folder and updated my application.js to include (right at the bottom, just above require tree):

//= require underscore
//= require dependent-fields


$(document).ready(function() {
    DependentFields.bind()
});

在我的简单表格中,我有:

In my simple form, I have:

<%= f.input :has_milestones, as: :boolean, boolean_style: :inline, :label => 'Any decision points?', id: 'check_project_milestones' %>

<%= content_tag :div, class: 'js-dependent-fields', data: {'checkbox-id': 'check_project_milestones', 'checkbox-value': 'true' } do %>        

  <div class="intpol2">
    Milestones
  </div>

  <%= f.simple_fields_for :milestones do |f| %>
    <%= render 'milestones/milestone_fields', f: f %>
  <% end %>
<% end %>

然而,当我重新启动服务器并呈现表单时,第一个具有div id的问题,显示(未选中复选框),内容标记内的内容也显示。我希望它被隐藏,直到选中复选框。

However, when I restart the server and render the form, the first question which has the div id, is shown (which the check box unticked) and the content inside the content tag is also showing. I want it to be hidden until the checkbox is checked.

我在driftingruby.com上找到了一个教程,展示了如何设置它。我唯一可以猜到的是,可能需要使用turbo链接才能完成这项工作?我禁用它因为我使用olark。

I found a tutorial on driftingruby.com which shows how to set this up. The only thing I can guess is that maybe turbo links is required to make this work?? I have it disabled because I use olark.

当我检查控制台是否有错误时,我可以看到这个看起来很相关:

When I inspect the console for errors, I can see this one, which appears to be relevant:

[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (underscore-min.map, line 0)

我还应该补充一点,我在回答我之前关于这个问题的问题时尝试了所有建议,但未能找到解决方案: Rails 4 - JS用于具有简单形式的相关字段

I should also add that I tried everything suggested in answer to my earlier question on this problem, but wasn't able to find a solution: Rails 4 - JS for dependent fields with simple form

谁能看到我做错了什么?

Can anyone see what I've done wrong?

推荐答案

你是什么重新体验是源映射。这允许您在使用缩小的JS文件时使用浏览器的开发人员工具中的可读代码进行调试。

What you're experiencing is source mapping. This allows you to debug with readable code in your browser's developer tools when working with minified JS files.

Underscore的缩小版本在文件末尾有此行:

The minified version of Underscore has this line at the end of the file:

//# sourceMappingURL=underscore-min.map

当遇到此行时,您的浏览器开发者工具将尝试下载 underscore-min.map

Your browser's developers tools will try to download underscore-min.map when encountering this line.

如果你想摆脱错误,可以:

If you want to get rid of the error, either:


  1. 从<$ c中删除该行$ c> underscore-min.js

  2. 将underscore-min.map和underscore.js添加到您的项目中。

这篇关于Rails 4 - 带有dependent-fields-rails的条件JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆