Ruby on Rails - 未定义的方法`split'代表nil:StocksController中的NilClass / NoMethodError #search
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby on Rails - 未定义的方法`split'代表nil:StocksController中的NilClass / NoMethodError #search相关的知识,希望对你有一定的参考价值。
我是Rails的新手,我正在Udemy上完成Ruby on Rails开发人员课程。当我尝试去/search_stocks
路线时,我收到以下错误。
这是当前状态下的回购(我也有下面粘贴的代码):https://github.com/sarahbasinger/rails-stock-tracker
这是Udemy课程回购:https://github.com/udemyrailscourse/finance-tracker
该课程的TA表明它可能是一个宝石版本冲突。我正在使用Rails 5.1.4(也许是一个新手的错误 - 我认为使用最新和最好的将是一个很好的方式去)。课程中的老师正在使用Rails 4. TA建议我使用与课程相同的宝石版本,所以我更新了我的Gemfile以匹配Gemfile课程,运行bundle install,然后我甚至无法获得rails服务器运行。我得到了一个不同的错误。所以我回过头来尝试使用Rails 5运行这个应用程序。但是,我没有尝试解决gem版本冲突的经验,如果这是问题。
这是相关的代码:
模型
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
调节器
class StocksController < ApplicationController
def search
@stock = Stock.new_from_lookup(params[:stock])
render json: @stock
end
end
视图
<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]
任何帮助表示赞赏!
在我看来,你需要一个名为“stock”的参数。在第4行
@stock = Stock.new_from_lookup(params[:stock])
它正在寻找一个名为“stock”的URL参数,它是一个股票代码。尝试类似的东西:
localhost:3000/search_stocks?stock=goog
这将创建一个股票参数并给它一些东西来查找。您还可以输入一些代码来处理param为零的情况。
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
现在我想到它,可能甚至更好地在模型中做到这一点:
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
我希望这有帮助!
您可以尝试以下操作,因为大多数时候gem功能更新,这就是为什么不在实现此代码。我最近在下面测试了这种类型的项目编写代码。
模型
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
希望能有所帮助
以上是关于Ruby on Rails - 未定义的方法`split'代表nil:StocksController中的NilClass / NoMethodError #search的主要内容,如果未能解决你的问题,请参考以下文章
Ruby on Rails:未定义的方法,检查boolean if true语句
Base64 编码的字符串到文件(Ruby on Rails) - 未定义的方法“解包”错误
Ruby on Rails Simple_form_for的未定义方法
Ruby on Rails - 未定义的方法`split'代表nil:StocksController中的NilClass / NoMethodError #search
nameerror 未定义的局部变量或方法 `log_out' 你的意思是? logout_url ruby on rails