RoR-股票报价宝石-无法检索数据

时间:2018-10-09 11:03:23

标签: ruby-on-rails

我正在尝试使用股票报价宝石来获取数据,以带有参数的形式提供股票报价,但我无法展示任何价值。

我的表格:

<h1>My portfolio</h1>
<h3>Search for Stocks</h3>

<div class="stock-lookup">
    <%= form_tag search_stocks_path, remote: true, method: :get, id:"stock-lookup-form", enforce_utf8: false 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 'users/result' %>
</div>

_result.html.erb部分:

<div class="results-block">
    <%= bootstrap_flash %>
</div>

<% if @stock %>
    <div class="well results-block">
        <strong>Symbol: </strong><%= @stock.ticker %>
        <strong>Name: </strong><%= @stock.name %>
        <strong>Price: </strong><%= @stock.last_price %>
        <%= link_to "Add to my stocks", user_stocks_path(user: current_user, stock_ticker: @stock.ticker), class: "btn btn-xs btn-success", method: :post %>
    </div>
<% end %>

_result.js.erb:

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

路线:

   Rails.application.routes.draw do
      devise_for :users
      # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
      root 'welcome#index'
      get 'my_portfolio', to: 'users#my_portfolio'
      get 'search_stocks', to: 'stocks#search', as: 'search_stocks'
      resources :user_stocks, only: [:create]
    end

stocks_controller.rb:

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
     respond_to do |format|
       format.js {render partial: 'users/result'}
     end



  end

end

和库存型号:

class Stock < ApplicationRecord
    has_many :user_stocks
    has_many :users, through: :user_stocks

    def self.find_by_ticker(ticker_symbol)
        where(ticker: ticker_symbol).first
    end

    def self.new_from_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: price)

        rescue Exception => e
            return nil
        end
    end

    def self.strip_commas(number)
        number.gsub(",", "")
    end

end

我正尝试通过以下图片展示股票的价值:

enter image description here

但是我所得到的只是库存控制器上显示的错误消息。看来我无法访问股票报价数据库。

1 个答案:

答案 0 :(得分:0)

找到了解决方案,只需在股票模型上的strip_commas方法上对.to_s进行广告即可,

def self.strip_commas(number)
  number.to_s.gsub(",", "")
end