Ajax响应成功,但页面未使用部分包含的响应进行更新

时间:2019-05-26 03:24:47

标签: jquery ruby-on-rails ruby ajax

我的Rails应用程序中的这个股票搜索工具正在运行,然后发生了一些事情(想想我做了什么,不确定是怎么回事),但现在却不起作用。 ajax调用成功,我什至可以在Chrome Dev Tools中看到响应,完全格式化以及所有内容。该HTML是局部的,应该在调用包含ajax的操作后填充页面。

部分结果

<% if @stock %>
    <div class="bg-grey-lightest mx-auto py-6 shadow shadow-lg w-1/2">
      <p class="flex justify-between px-16 py-3 normal-font"><strong>Symbol:</strong> <%= @stock.company_name%></p>
      <p class="flex justify-between px-16 py-3 normal-font"><strong>Name:</strong> <%= @stock.symbol%></p>
      <p class="flex justify-between px-16 py-3 normal-font"><strong>Price:</strong> <%= @stock.latest_price%>&nbsp;</p>
<% end %>

主要库存查询页面

      <%= form_tag search_stocks_path, remote: true, method: :get do %>
      <div id="stock-lookup-form">
        <div>
          <div class="w-96 sm:w-full mr-1 sm:mr-0 sm:mb-1 z-20">
            <%= text_field_tag :stock, params[:stock], placeholder: "Stock ticker symbol", autofocus: false,
                  class: "bg-grey-lighter appearance-none border-2 border-yellow-offset hover:border-grey-dark rounded-sm py-4 px-6 sm:w-1/2 md:1/4 lg:w-1/4 text-black text-center" %>
          </div>
          <div>
            <%= button_tag(type: :submit, class: "bg-white block border border-red-darker hover:bg-red-darkest burgandy hover:text-red-lightest mx-auto my-4 no-underline normal-font py-3 rounded shadow-lg w-48") do %>
            <i class="fa fa-search"></i> Search Stock Prices
            <% end %>
          </div>
          <% end %>
          <div id="results">
            <%= render 'layouts/result' %>
          </div>
        </div>
      </div>

<script>
$(document).ready(function(){
  $('#stock-lookup-form').on('ajax:complete', function(event, data, status){
    $('#results').html(data.responseText);
  });
}); 
</script>

采取行动

class StocksController < ApplicationController

  def search
    if params[:stock].blank?
      flash.now[:danger] = "You have entered an empty search string"
    else
      @stock = Stock.new_from_lookup(params[:stock])
      flash.now[:danger] = "You have entered an incorrect symbol" unless @stock
    end
    render partial: 'layouts/result'
  end
end

就像我说的那样,请求成功,控制台中没有错误,并且响应看起来完全像我想要的那样。局部div只是保持为空,不会更新。

Imgur Imgur

0 个答案:

没有答案