简体   繁体   English

Ruby on Rails无法加载Javascript

[英]Ruby on rails not loading Javascript

I had this working, but must have made a minor change somewhere that's stopped it working. 我已经完成了这项工作,但必须在停止其工作的地方进行了较小的更改。

I have a call to Javascript when clicking on a button, but when I click the button nothing loads. 单击按钮时,我会调用Javascript,但是单击按钮时,不会加载任何内容。 I've gone over all parts of the code multiple times but cannot see where the failure is happening and there's no feedback in the logs so I have nothing to go on. 我已经遍历了代码的所有部分,但是无法看到发生故障的地方,并且日志中也没有反馈,因此我无事可做。

As I said it was working previously, but then stopped. 正如我所说的,它以前工作正常,但随后停止了。

This is my button: 这是我的按钮:

            <%= button_tag t(".change"), class: "btn let-admin-change",
              data: { title: t(".confirm_switch"),
                      cancel: t(".cancel"),
                      cta: t(".confirm_cta"),
                      plot: plot.id } %>

And my javascript file: 和我的JavaScript文件:

(function (document, $) {
  'use strict'

  $(document).on('click', '.let-admin-change', function (event) {
    var dataIn = $(this).data()

    var $changeContainer = $('.change-admin-letting-form')

    $('body').append($changeContainer)
    var $form = $('.edit_plot')

    $changeContainer.dialog({
      show: 'show',
      modal: true,
      width: 700,
      title: dataIn.title,
      buttons: [
        {
          text: dataIn.cancel,
          class: 'btn',
          click: function () {
            $(this).dialog('destroy')
          }
        },
        {
          text: dataIn.cta,
          class: 'btn-send btn',
          id: 'btn_submit',
          click: function () {

            $.ajax({
              url: '/plots/' + dataIn.plot,
              data: $form.serialize(),
              type: 'PUT'
            })

            $(this).dialog('destroy')
            $changeContainer.hide()
          }
        }]
    }).prev().find('.ui-dialog-titlebar-close').hide() // Hide the standard close button
  })

})(document, window.jQuery)

And the form it's calling: 和它调用的形式:

<div class="change-admin-letting-form">
  <%= simple_form_for plot, url: phase_lettings_path(@phase), remote: true do |f| %>
    <div>
      <%= f.hidden_field :letter_type, value: :homeowner %>
      <%= f.hidden_field :letable_type, value: :planet_rent %>
    </div>
    <div>
      <h3><%= t(".are_you_sure", plot: plot) %></h3>
      <p><%= t(".confirm_admin_swap").html_safe %></p>
    </div>
  <% end %>
</div>

Can anyone see where this is going wrong to stop the javascript from loading? 谁能看到这在哪里出错以阻止javascript的加载?

EDIT 编辑

If I add 如果我加

 <%= render "change_admin_letting_form", plot: plot %>

underneath the button then the form loads (but it loads multiple forms - one form for each instance of the button / each plot on the page), so I assume the issue is that the plot information is not being passed to the form? 在按钮下面然后加载表单(但是它加载多个表单-每个按钮实例/页面上每个图的一个表单),所以我认为问题是图信息未传递到表单? I'm not sure how to pass this information across. 我不确定如何传递这些信息。

I've managed to get this working by doing the following to get the correct information to the form: 我设法通过执行以下操作以将正确的信息获取到表格中来使其工作:

I've changed the class of the form to take a variable: 我将表单的类更改为采用变量:

<div class="hidden <%= plot_id %>">

And send the variable to the form by rendering the form from inside the block that loads each plot: 通过从加载每个图的块内部渲染表单,将变量发送到表单:

<% plots.each do |plot| %>
  // some omitted code
  <%= button_tag t(".change"), class: "btn let-admin-change",
     data: { title: t(".confirm_switch"),
     cancel: t(".cancel"),
     cta: t(".confirm_cta"),
     plot: plot.id } %>
  <%= render "change_letter_type_form", plot_id: "change_letting_plot_#{plot.id}", plot: plot %>
<% end %>

And changed the part of the javascript that loads the form to search for the new plot-based class: 并更改了javascript加载表单的部分以搜索新的基于图的类:

var $changeContainer = $('.change_letting_plot_' + dataIn.plot)

So now only the one form relevant to the selected plot loads, and the form works as required. 因此,现在只有一种与所选图样负载相关的表格,并且该表格可以按要求工作。

I have next to zero experience with javascript, so this probably isn't the 'proper' way to get this to work, but it works so I'll take it as it is. 我几乎没有使用javascript的零经验,所以这可能不是使其正常工作的“正确”方法,但是它可以工作,因此我将按原样进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM