skip to Main Content

I’m a Rails newbie and I’m taking the Complete Ruby on Rails Developer Course on Udemy. I’m getting the following error when I try to go to the /search_stocks route.

enter image description here

Here’s the repo in its current state (I also have the code pasted below): https://github.com/sarahbasinger/rails-stock-tracker

Here’s the Udemy course repo:
https://github.com/udemyrailscourse/finance-tracker

The TA for the course suggests it might be a gem version conflict. I’m using Rails 5.1.4 (maybe a newbie mistake – I thought using the latest and greatest would be a good way to go). The teacher in the course is using Rails 4. The TA suggested I use the same gem versions as the course, so I updated my Gemfile to match the course Gemfile, ran bundle install, and with that, I can’t even get the rails server to run. I get a different error. So I’m back to trying to get this app running using Rails 5. However, I have no experience trying to resolve gem version conflicts, if that is the issue.

Here’s the relevant code:

Model

class Stock < ActiveRecord::Base

    def self.new_from_lookup(ticker_symbol)
        looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
        new(name: looked_up_stock.name, ticker: looked_up_stock.symbol, last_price: looked_up_stock.l)
    end
end

Controller

class StocksController < ApplicationController

    def search
        @stock = Stock.new_from_lookup(params[:stock])
        render json: @stock
    end
end

View

<h1>My portfolio</h1>
<h3>Search for stocks</h3>
<div id="stock-lookup">
<%= form_tag search_stocks_path, 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>

Gemfile

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
gem 'devise'
gem 'twitter-bootstrap-rails'
gem 'jquery-rails'
gem 'devise-bootstrap-views'
gem 'stock_quote'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  gem 'sqlite3'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'

end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'

end

group :production do
  gem 'pg'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Any help is appreciated!

2

Answers


  1. It looks to me that you need a parameter called “stock”. In line 4

    @stock = Stock.new_from_lookup(params[:stock])
    

    It is looking for a URL parameter called “stock” which is a ticker symbol. Try something like:

    localhost:3000/search_stocks?stock=goog
    

    That will create a stock param and give it something to look up. You could also put in some code to handle the case where the param is nil.

    def search
        if params[:stock]
            @stock = Stock.new_from_lookup(params[:stock])
        else
            # you should have some directions here for what happens if there is no stock param given.
            @stock = nil
        end
        render json: @stock
    end
    

    Probably even better to do that in the model, now that I think of it:

    def self.new_from_lookup(ticker_symbol)
        if ticker_symbol
            looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
        else
            # something here for a missing stock param
            looked_up_stock = Stock.first
        end
        new(name: looked_up_stock.name, ticker: looked_up_stock.symbol, last_price: looked_up_stock.l)
    end
    

    I hope this helps!

    Login or Signup to reply.
  2. You can try the following because most of the time gem functionality updating that’s why not working on implementing this code. I’ve recently worked this type of project write code the below which is tested.

    Model

    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.l)
            new(name: looked_up_stock.name, ticker: looked_up_stock.symbol, last_price: price)
        rescue Exception => e
            return nil
        end
    end
    
    def self.strip_commas(number)
        number.gsub(",", "")
    end
    

    Hope to help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search