weixin_33693070 2018-08-03 12:56 采纳率: 0%
浏览 34

Ajax在rails 5中不起作用

i use stock gem to search name of company and return last-price.when add Ajax not return any things. no error in console but when open network appear message failed to load data .

stock_controller.rb

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

    else
        flash[:danger] = "You have entered an empty search string"
        redirect_to my_portfolio_path
    end
end

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 text-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(type: :submit, class: "btn btn-lg btn-success") do %>
          <i class="fa fa-search"></i> Look up a stock
        <% end %>
      </div>
    </div>
  <% end %>
</div>
<div id="results">
  <%= render 'result' %>
</div>

_result.html.erb

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

    </div>
<%end%>

_result.js.erb

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

when test file _result.js.erb add alert("hello"); work correctly.

setp of add ajax

  • add remote: true in form
  • add JQuery in application.js
  • use format.js and partial:
  • create file _result.js.erb

stock model

    def self.new_form_lookup(ticker_symbol)
    begin

        looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
        #price = strip_commas(looked_up_stock.latest_price) 
        new(name: looked_up_stock.company_name, 
            ticker: looked_up_stock.symbol, last_price: looked_up_stock.latest_price)

    rescue Exception => e 
        return nil
    end
end

log of output

log

  • 写回答

1条回答 默认 最新

  • weixin_33744141 2018-08-04 12:56
    关注

    where do you find @stack ?

    you only respond with js if @stack is not nil

    what is the value of @stack and where does it get that value?

    def search
    if params[:stock].present?
        @stock = Stock.new_form_lookup(params[:stock])
        if @stack 
            respond_to do |format|
              format.js { render partial: 'users/result' }
            end
        else
            flash[:danger] = "You have entered an incorrect symbol"
            redirect_to my_portfolio_path
        end
    
    else
        flash[:danger] = "You have entered an empty search string"
        redirect_to my_portfolio_path
    end
    
    评论

报告相同问题?