Javascript Rails 4-JS用于具有简单表单的依赖字段

Javascript Rails 4-JS用于具有简单表单的依赖字段,javascript,jquery,ruby-on-rails,underscore.js,simple-form,Javascript,Jquery,Ruby On Rails,Underscore.js,Simple Form,我正在尝试在Rails 4中制作一个应用程序 "Be sure to include underscorejs and jquery in your page." 我使用简单表单作为表单,并且刚刚尝试使用gem“dependent fields rails”来隐藏或显示基于主要问题表单字段的子集问题 "Be sure to include underscorejs and jquery in your page." 我被卡住了 "Be sure to include underscorejs

我正在尝试在Rails 4中制作一个应用程序

"Be sure to include underscorejs and jquery in your page."
我使用简单表单作为表单,并且刚刚尝试使用gem“dependent fields rails”来隐藏或显示基于主要问题表单字段的子集问题

"Be sure to include underscorejs and jquery in your page."
我被卡住了

"Be sure to include underscorejs and jquery in your page."
我已将gem添加到我的gem文件中,用于:

gem 'dependent-fields-rails'
gem 'underscore-rails'
"Be sure to include underscorejs and jquery in your page."
我已将application.js更新为:

//= require dependent-fields
//= require underscore
"Be sure to include underscorejs and jquery in your page."
我有一张表格,上面有:

<%= f.simple_fields_for :project_date do |pdf| %>
  <%= pdf.error_notification %>

                <div class="form-inputs">


                    <%= pdf.input :student_project, as: :radio_buttons, :label => "Is this a project in which students may participate?", autofocus: true %>


                    <div class="js-dependent-fields" data-radio-name="project_date[student_project]" data-radio-value="true">


                    <%= pdf.input :course_project, as: :radio_buttons, :label => "Is this a project students complete for credit towards course assessment?" %>

                    <%= pdf.input :recurring_project, as: :radio_buttons, :label => "Is this project offered on a recurring basis?" %>

                    <%= pdf.input :frequency,  :label => "How often is this project repeated?", :collection => ["No current plans to repeat this project", "Each semester", "Each year"] %>
                    </div>

                    <div class='row'>
                        <div class="col-md-4">
                            <%= pdf.input :start_date, :as => :date_picker, :label => "When do you want to get started?"  %>
                        </div>  
                        <div class="col-md-4">
                            <%= pdf.input :completion_date,  :as => :date_picker, :label => "When do you expect to finish?" %>
                        </div>
                        <div class="col-md-4">          
                            <%= pdf.input :eoi, :as => :date_picker, :label => 'When are expressions of interest due?' %>
                        </div>
                    </div>
                </div>  
        <% end %>

<script type="text/javascript">
  $('.datetimepicker').datetimepicker();
</script>

<script>
$(document).ready(function() {
    DependentFields.bind()
});
</script>
"Be sure to include underscorejs and jquery in your page."
如何在页面中包含下划线和jQuery?我的gem文件里有。这就足够了,还是需要其他东西来完成这项工作

"Be sure to include underscorejs and jquery in your page."
目前,当我尝试此表单时,没有隐藏任何内容。我曾尝试将真实值替换为“是”,但这也没有任何区别

<div class="js-dependent-fields" data-radio-name="project_date[student_project]" data-radio-value="true">

<div class="js-dependent-fields" data-radio-name="project_date[student_project]" data-radio-value="yes">
"Be sure to include underscorejs and jquery in your page."

有人能看出我哪里出了错吗

"Be sure to include underscorejs and jquery in your page."
Rails会自动执行此操作。它在安装时将jQuery添加到application.js文件中,因此如果您尚未删除该行,我们可以跳到步骤2:

"Be sure to include underscorejs and jquery in your page."
您需要在页面中包含
application.js
脚本。您可以在正在处理的视图中执行此操作,但是如果您在应用程序的其他地方使用javascript,我建议将javascript添加到应用程序的布局中,以便将其应用到使用该布局的所有页面

"Be sure to include underscorejs and jquery in your page."
默认情况下,该文件位于
app/views/layouts/application.html.erb

"Be sure to include underscorejs and jquery in your page."
必须将这行代码(或类似代码)添加到文件中,才能将脚本加载到视图中:

"Be sure to include underscorejs and jquery in your page."

"Be sure to include underscorejs and jquery in your page."
它将允许您访问所有javascript代码

"Be sure to include underscorejs and jquery in your page."
编辑:

"Be sure to include underscorejs and jquery in your page."
除此之外,我注意到您缺少用于显示字段的子句。在dependent字段中,您必须指定您所依赖的字段以及元素类上的值,如下所示:

"Be sure to include underscorejs and jquery in your page."
<div class="js-dependent-fields[data-select-id='filing_<INPUT_ID>' data-option-value='<VALUE>']"> 
   ... fields ...
</div>

... 领域。。。

您应该将
替换为这些字段所依赖的输入id,并将
替换为
必须具有的相应值。

我认为您在定义单选按钮时跳过了
集合
属性

"Be sure to include underscorejs and jquery in your page."
如果查看,您将看到他们使用
集合
来定义单选按钮的值。然后,它们使用定义的一个值来设置
数据无线电值
属性

"Be sure to include underscorejs and jquery in your page."
试试这个,让我知道:

"Be sure to include underscorejs and jquery in your page."
<div class="form-inputs">

  <%= pdf.input :student_project, as: :radio_buttons, autofocus: true,
      :label => "Is this a project?", :collection => ['Yes', 'No'] %>

  <div class="js-dependent-fields" data-radio-value='Yes' 
       data-radio-name="project_date[student_project]">
    ...
  </div>

</div>
虽然示例项目以这种方式需要库:

"Be sure to include underscorejs and jquery in your page."
//= require dependent-fields
//= require underscore
//= require underscore
//= require dependent-fields

希望这有帮助

非常感谢你的解释。我的application.html.erb中已经有:false%>了,所以看起来不需要任何更改。然而,肯定还有别的地方出了问题,因为隐藏显示功能不起作用。嗨,乔纳斯,这正是我在表格中所做的。我也尝试过改变true/yes值,但是没有任何区别,字段在依赖的字段为true之前不会隐藏。您认为可能是这样的:$(document.ready(function(){DependentFields.bind()});不需要在脚本标记中。或者可能我自己不需要插入它(可能gem本身就需要插入)?或者-可能我想在脚本标记中指定脚本类型-例如:???嗨,丹尼尔,我试过了(如果答案是否定的,则将…替换为我想隐藏的字段-但ti不起作用。字段不会根据问题隐藏/显示。@user2860931 Mmm,这很奇怪。您在浏览器的控制台中看到任何错误吗?可能该控制器的方法使用了与应用程序不同的其他布局,但我是在黑暗中拍摄的。if
jQuery
下划线在该视图中不是必需的,它应该会在浏览器控制台或服务器日志中向您显示错误消息。您能检查一下吗?这样我就可以帮助您了!@user2860931我才意识到,您的代码可能无法工作,因为您需要
application.js
中的库的顺序我会更新我的答案以加深对它的了解。嗨,丹尼尔,我尝试在application.js中更改顺序。谢谢你的建议,但我仍然有相同的答案problem@user2860931在Jonas的回答中,您评论说您已经在
application.html.erb
中包含了
application.js
,但是您确定控制器中使用的布局是应用程序吗?…再说一遍,也许你应该用日志更新你的问题以帮助你
"Be sure to include underscorejs and jquery in your page."