简体   繁体   English

Rails-添加.js.erb文件时不会加载Ajax请求

[英]Rails - Ajax request doesn't load when I add .js.erb file

I'm trying to do a simple website, a stock tracker if you will. 我正在尝试制作一个简单的网站,如果可以的话,可以使用股票追踪器。 One of the features is getting the results using ajax, as not to load the entire page, I can go as far as to get the html with the data I want into the response, but cannot load it into the page. 功能之一是使用ajax获取结果,因为不加载整个页面,我可以尽可能地将包含所需数据的html获取到响应中,但不能将其加载到页面中。 And when I add the equivalent js.erb file, I get no response at all. 当我添加等效的js.erb文件时,我根本没有任何响应。

Here's the page: (my_portfolio.html.erb) 这是页面:(my_portfolio.html.erb)

    <h1> My Portfolio</h1>

<h3>Search for stocks</h3>
  <div id="stock-lookup">
    <%= form_tag search_stocks_path, remote: true, method: :get, id: "stock-lookup-form" do %>
    <div class="form-group row no-padding center col-md-12">
      <div class="col-md-10">
        <%= text_field_tag :stock, params[:stock], placeholder: "Stock ticker symbol", autofocus: true,
                           class: "form-control search-box input-lg" %>
      </div>
      <div class="col-md-2">
        <%= button_tag :submit, class: "btn btn-lg btn-success" do %>
          <%= fa_icon "search" %> Look up a stock

        <% end %>
      </div>


    </div>
    <% end %>

  </div>
  <div id="results">
    <%= render 'users/result' %>
  </div>

here's the partial that gets rendered at the bottom: (_result.html.erb) 这是在底部呈现的部分内容:(_result.html.erb)

<% if @stock %>
  <div class="well results-block">
    <strong>Symbol: </strong><%= @stock.ticker %>
    <strong>Name: </strong><%=@stock.name %>
    <strong>Price: </strong><%= @stock.last_price %>
  </div>
<% end %>

here's the method that runs when you submit the form (stocks_controller.rb): 这是您提交表单(stocks_controller.rb)时运行的方法:

class StocksController < ApplicationController
  def search

    if params[:stock].present?
      @stock = Stock.new_from_lookup(params[:stock])
      if @stock
        respond_to do |format|
          format.js { render partial: 'users/result' }
        end
      else
        flash[:danger] = "You have entered an invalid symbol."
        redirect_to my_portfolio_path
      end

    else
      flash[:danger] = "You have input an unexistant stock."
      redirect_to my_portfolio_path
    end

  end
end

here's my problem: when I submit a valid input, I check the server response in the browser and there I see the response text: 这是我的问题:提交有效的输入时,我在浏览器中检查服务器响应,然后在其中看到响应文本:

  <div class="well results-block">
    <strong>Symbol: </strong>GOOG
    <strong>Name: </strong>Alphabet Inc.
    <strong>Price: </strong>1042.1
  </div>

So that means that i am getting the data back, however, trying to put it on the page is not working, because when i create the appropriate _result.js.erb , the request does not return anything at all and the page isn't populated with the data, the request only returns 200 when there is no js.erb file. 因此,这意味着我要取回数据,但是,尝试将其放到页面上是行不通的,因为当我创建适当的_result.js.erb ,请求根本不会返回任何内容,并且页面也不会返回填充数据后,当没有js.erb文件时,请求仅返回200。 here's the code: (_result.js.erb) 这是代码:(_result.js.erb)

$('#results').html("<%= j (render 'users/result') %>")

I have no idea what the error could possibly be. 我不知道错误可能是什么。 Could anyone shed a light on this? 有人可以阐明这一点吗?

Edit: The approved answer works, yes, but I found that you can also pass a render to change page content, and that $('#results').html("<%= j (render 'users/result') %>") actually tries to render the page, while the actual intent is just to get the html, for that, simply use $('#results').html("<%= j (render partial:'users/result.html.erb') %>") instead. 编辑:批准的答案有效,是的,但是我发现您还可以传递渲染以更改页面内容,并且$('#results').html("<%= j (render 'users/result') %>")实际上试图呈现页面,而实际意图只是获取html,为此,只需使用$('#results').html("<%= j (render partial:'users/result.html.erb') %>") However, the html.erb part seems strictly necessary, unlike everywhere else in rails. 但是, html.erb部分似乎严格必要,与其他地方不同。 It'd be great if someone could point out why. 如果有人能指出原因,那就太好了。

This seems like you are close and the issue is conflating server rendered versus client rendered. 看来您已经关闭,问题在于将服务器渲染与客户端渲染进行合并。 Your javascript example of 您的javascript示例

$('#results').html("<%= j (render 'users/result') %>")

Will only be rendered 1 time on the server, so it will have no impact on the client when you get a response from an ajax call. 将仅在服务器上呈现1次,因此当您从ajax调用获得响应时,它对客户端没有任何影响。

For this to work it needs to be part of the ajax or fetch call and should looks something like this. 为此,它必须是ajax或fetch调用的一部分,并且应该看起来像这样。

html_results_string = "YOUR RAW HTML RESPONSE"
$('#results').html(html_results_string)

Side note, i recommend against the id here as well. 旁注,我也建议您不要在此输入ID。 Use something like a data- selector based on the id of the thing you are requesting. 根据要请求的事物的ID使用类似数据选择器的事物。

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

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