简体   繁体   English

具有Javascript和Ajax的Rails开发环境

[英]Rails Dev environment with Javascript and Ajax

There's something I definitely don't understand going on in the background in dev. 在开发人员的后台进行某些工作我绝对不理解。

I have an app which I've been developing locally on my Mac, and it has a form which makes an Ajax call "remote: true". 我有一个我在Mac上本地开发的应用程序,它的形式使Ajax称为“ remote:true”。 It stopped working, no Ajax call, nothing in the logs. 它停止工作,没有Ajax调用,日志中没有任何内容。

The only way I could get it to start working again was to edit my _form.html.erb partial by adding a second input field (identical to the original, straight copy and paste). 我可以使它再次开始工作的唯一方法是通过添加第二个输入字段(与原始文本相同,直接复制并粘贴)来编辑_form.html.erb部分。 Refresh the page, then it worked. 刷新页面,然后就可以了。 I've unedited my original edit, so the code is back to where it originally was (when it was not working), but now it's working. 我没有编辑我的原始编辑内容,因此代码恢复到了原来的位置(当它不起作用时),但是现在可以正常工作了。

What does the edit cause to happen? 编辑会导致什么结果? Is there someway I can cause it to happen without editing my code? 有什么办法可以使它发生而无需编辑代码吗?

Thanks. 谢谢。

----- EDIT adding code ----- -----编辑添加代码-----

My form is (the text_field_tag is what I copied and pasted) 我的形式是(text_field_tag是我复制并粘贴的内容)

  <div id="friend-lookup">
  <h3>Search for friends</h3>
  <%= form_tag search_friends_path, remote: true, method: :get, id: 'friend-lookup-form' do %>
    <div class="form-group row no-padding text-center col-md-12">
      <div class="col-md-10">
        <%= text_field_tag :search_param, params[:search_param], 
                                    placeholder: "first name, last name or email", autofocus: true,
                                    class: 'form-control search-box input-lg' %>
      </div>
      <div class="col-md-2">
        <%= button_tag(type: :submit, class: "btn btn-lg btn-success") do %>
          <i class="fa fa-search"></i> Look up a friend
        <% end %>
      </div>
    </div> <!--- form-group -->
  <% end %>
  <%= render 'common/spinner' %>
  <% if @users %>
    <% if @users.size > 0 %>
      <div id="friend-lookup-results" class="well results-block col-md-10">
        <table class="search-results-table col-md-12">
          <tbody>
            <% @users.each do |user| %>
            <tr>
              <td><strong>Name:</strong> <%= user.full_name %></td>
              <td><strong>Email:</strong> <%= user.email %></td>
              <td><strong>Profile:</strong> <%= link_to "View Profile", user_path(user),
                                                        class: "btn btn-xs btn-success" %>
                <% if current_user.not_friends_with?(user.id) %>
                  <%= link_to "Add as my friend", add_friend_path(user: current_user, friend: user),
                                                    class: "btn btn-xs btn-success", method: :post %>
                <% else %>
                  <span class="label label-primary">
                    You are friends
                  </span>
                <% end %>
              </td>
            </tr>
            <% end %>
          </tbody>
        </table>
      </div>
    <% else %>
      <p class="lead col-md-12">
        No people match this search criteria
      </p>
    <% end %>
  <% end %>
  <div id="friend-lookup-errors"></div>
</div>

And the javascript is 而JavaScript是

# assets/javascript/friends.js

var init_friend_lookup;

init_friend_lookup = function() {
  $('#friend-lookup-form').on('ajax:before', function(event, data, status){
    $('#friend-lookup-results').replaceWith(' ');
    show_spinner();
  });

  $('#friend-lookup-form').on('ajax:after', function(event, data, status){
    hide_spinner();
  });

  $('#friend-lookup-form').on('ajax:success', function(event, data,status){
    $('#friend-lookup').replaceWith(data);
    init_friend_lookup();
  });

  $('#friend-lookup-form').on('ajax:error', function(event, xhr, status, error){
    hide_spinner();
    $('#friend-lookup-results').replaceWith(' ');
    $('#friend-lookup-errors').replaceWith('Person was not found.');
  });
}

$(document).ready(function() {
  init_friend_lookup();
});

You may be used to installing JavaScript behavior in response to the window.onload, DOMContentLoaded, or jQuery ready events. 您可能习惯于安装JavaScript行为以响应window.onload,DOMContentLoaded或jQuery ready事件。 With Turbolinks, these events will fire only in response to the initial page load—not after any subsequent page changes. 使用Turbolinks,这些事件仅在响应初始页面加载时才会触发,而不是在随后的任何页面更改之后触发。

Change the "ready" event for 'turbolinks:load', and repeat the same step with you all events. 更改“ turbolinks:load”的“就绪”事件,并对所有事件重复相同的步骤。

$(document).on('turbolinks:load', function () {
    init_friend_lookup();
});

Read the documentation, https://github.com/turbolinks/turbolinks 阅读文档https://github.com/turbolinks/turbolinks

Maybe this will help someone out there. 也许这会帮助某个人。

I am running Rails 5. 我正在运行Rails 5。

What was tricky was that it seemed intermittent. 棘手的是,它似乎是断断续续的。

My problem seems to be turbolinks related. 我的问题似乎与turbolinks有关。 Removing turbolinks , in javascript/application.js 删除javascript / application.js中的 turbolinks

//= require turbolinks // =需要turbolinks

seems to have fixed the problem consistently. 似乎已经一致地解决了这个问题。

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

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