简体   繁体   English

保存并重新加载后,Rails Server仅成功运行一次表单

[英]Rails Server runs form successfully only one after save and reload

I have 2 pieces of code that will only run once when I make a change and save the code in the form and then reload the form. 我有2条代码,当我进行更改并将代码保存在表单中然后重新加载表单时,它们只会运行一次。 The next time I open and run the form it fails to run. 下次我打开并运行该窗体时,它将无法运行。 Every other part of my application runs just fine. 我的应用程序的其他所有部分都运行良好。

The first is a datepicker: 第一个是日期选择器:

The datepicker only shows up the first time. 日期选择器仅在第一次出现。

Here is the coffee script: 这是咖啡脚本:

$.fn.extend {
  integrateDatepicker: (selector)->
    selector = selector || '.datepicker'
    $(@).find(selector).datepicker({"format": "dd/mm/yyyy"}) 
}
$(document).ready () ->
  $('body').integrateDatepicker()

Here is the form snippet: 这是表单片段:

<div class="row">
  <div class="span6 offset3">
    <%= simple_form_for([@council_report]) do |f| %>
      <p>
        <%= f.input :report_title %>
      </p>
      <p>
        <%= f.input :report_desc %>
      </p>
      <p>
        <%= f.input :report_date, as: :string, input_html: {class: 'datepicker'} %>
      </p>       
      <p>
        <%= f.submit  class: "btn btn-large btn-primary" %>
      </p>
    <% end %>
  </div>
</div>

The second is a form post: 第二个是表单发布:

The "create" action runs successfully only the first time the form loads. 仅在第一次加载表单时,“创建”操作才能成功运行。

Here is the form snippet: 这是表单片段:

<%= form_for(:select_asset, url: {action: "create"}) do |form| %>
  <% @assets_not_in.each_with_index do |asset_not_in, index|
       asset = Asset.find(asset_not_in.id) %>
       <%= fields_for "assets_to_check[#{index}]", asset_not_in do |f| %>
         <%= f.hidden_field :id, :value => asset.id %>
         <tr>
           <td><%= asset.asset_name %></td>
           <td><%= asset_type_desc(asset.asset_type_code) %></td>
           <td><%= asset.address.address_long %></td>
           <td><%= asset.address.postcode %></td>
           <td><%= asset.address.state_code %></td>
           <td><%= f.check_box :add_asset %></td>
         </tr>
       <% end %>
  <% end %>
</table>

  <%= link_to 'Back', council_report_report_assets_path(@council_report) %>
  <%= form.submit "Add Assets", class: "btn btn-large btn-primary" %>
<% end %>

Questions: 问题:

  1. Has anyone out there experienced this behaviour? 有没有人经历过这种行为?
  2. What is wrong with my application causing this bizaire behaviour? 我的应用导致这种bizaire行为出了什么问题?
  3. Of course, how do I fix it? 当然,我该如何解决?

Turbolinks is enabled by default in rails 4 and will cause issues with jQuery's $(document).ready . Turbolink默认在rails 4中启用,并且会导致jQuery的$(document).ready

To verify you're runnign turbolinks, check your app/views/layouts/application.html.erb . 要验证您是否运行正常的Turbolink,请检查您的app/views/layouts/application.html.erb Look for the lines 寻找线

<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

in the head . head If they are set to true, try adding the jquery-turbolinks gem to your Gemfile. 如果将它们设置为true,请尝试将jquery-turbolinks gem添加到您的Gemfile中。

Turbolinks renders some parts of the page via ajax, so $(document).ready will not fire unless you enter the site through that page OR do a hard refresh. Turbolinks通过ajax渲染页面的某些部分,因此$(document).ready不会触发,除非您通过该页面进入该网站或进行强制刷新。 The gem fixes this by automatically changing your $(document).ready listeners: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks gem通过自动更改$(document).ready侦听器来解决此问题: http : //edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks

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

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