这样做是应用程序获得项目的Steam市场价格。我不小心删除了database.db,从那时起,同样的错误一直在弹出。我想知道代码有什么问题。也许我忽略了代码的某些部分。
问题出现在index.erb文件中。
require 'bundler/setup'
Bundler.require
require 'sinatra/reloader' if development?
require 'open-uri'
require "sinatra/json"
require 'uri'
require 'json'
require './models/bookmark.rb'
#require './models.rb'
enable :sessions
post '/signup' do
@user = User.create(mail:params[:mail], password:params[:password],
password_confirmation:params[:password_confirmation])
if @user != nil
session[:user]=@user.id
end
redirect '/'
end
post '/signin' do
user = User.find_by_mail(params[:mail])
if user && user.authenticate(params[:password])
session[:user] = user.id
end
redirect '/'
end
get '/signout' do
session[:user] = nil
redirect '/'
end
get '/' do
userid = session[:user]
if userid==nil
redirect '/home'
else
@user = User.find_by_id(userid)
@bookmark = @user.bookmarks
end
erb :inde1x
end
=begin
userid = session[:user]
if userid==nil
redirect '/home'
else
@user = User.find_by_id(userid)
@bookmarks = @user.bookmarks
end
=end
get '/signin' do
erb :signin
end
get '/signup' do
erb :signup
end
#get '/' do
# @bookmarks = Bookmark.all
#erb :index
#end
post '/create' do
url1 = params[:url]
url2 = URI.escape(url1)
priceurl = Nokogiri::HTML.parse(open('http://steamcommunity.com/market/search?appid=730&q='+url2))
erlength= priceurl.css('#searchResultsRows').text
if erlength.length >150
priceurl2= p priceurl.search("#resultlink_0")[0].attributes["href"].text
texturl= priceurl2[46..-1]
else
redirect '/'
end
redirect '/api/site?url='+texturl
end
get '/api/site' do
rawurl = params[:url]
url1 = URI.escape(rawurl)
# url2 = URI.escape(url1)
logger.info url
price = open('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+url1).read
data = JSON.load(price)
pricecheck= data['median_price']
#priceooo= pricecheck[1..6]
imagerine = Nokogiri::HTML.parse(open('https://steamcommunity.com/market/listings/730/'+url1))
imagebomb= imagerine.at_css('.market_listing_largeimage img')['src']
images= '<img src="'+imagebomb+'"/>'
html = Nokogiri::HTML.parse(open('https://steamcommunity.com/market/listings/730/'+url1))
title = html.css('title').text
titles = title.sub(/^Steam Community Market :: Listings for / , '')
Bookmark.create(title: titles, url: " Average Price: "+ pricecheck, pricecheck: images, user_id: session[:user] )
redirect '/'
end
post '/delete/:id' do
Bookmark.find(params[:id]).destroy
redirect '/'
end
post '/usercreateauth' do
Bookmark.create(title: params[:password], url: params[:username])
#redirect '/'+params[:username]
end
get '/home' do
erb :home
end
出于某种原因,&#34;每个&#34;不再是一种方法。
<div class="main">
<div id="app">
<h4>Input the item's name into the box</h4>
<p>Ex: Awp Asiimov Well Worn, </p>
<div style="height:20vh; overflow:hidden;"><img style="width:20vw;" src="/name.png"/></div>
<form action="/create" method="post" id="frm" class="">
<!--label for="url">Hash Value</label>
<input name="url" id="url"-->
<label for="url">Item Name</label>
<input name="url" id="url">
<input type="submit" value="Submit" name="commit" class="btn btn-primary submit"></input>
</form>
<br>
<div class="container-fluid">
<div class="row">
<% @bookmarks.each do |bookmark| %>
<div class="containerbox">
<p><%= bookmark.pricecheck%></p>
<p><%= bookmark.title %><b id="price"><%= bookmark.url %></b></p>
<span>
<form action="/delete/<%= bookmark.id %>" method="post">
<input class="delete" type="submit" value="x">
</form>
</span>
</div>
<% end %>
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL']||"sqlite3:db/development.db")
class Bookmark < ActiveRecord::Base
belongs_to :user
end
unless ENV['RACK_ENV'] == 'production'
ActiveRecord::Base.establish_connection("sqlite3:db/development.db")
end
class User < ActiveRecord::Base
has_many :bookmarks
has_secure_password
validates :password,
length: {in: 1..10}
end
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL']||"sqlite3:db/development.db")
class Bookmark < ActiveRecord::Base
belongs_to :user
end
unless ENV['RACK_ENV'] == 'production'
ActiveRecord::Base.establish_connection("sqlite3:db/development.db")
end
class User < ActiveRecord::Base
has_many :bookmarks
has_secure_password
validates :password,
length: {in: 1..10}
end
答案 0 :(得分:1)
我在App.rb
中注意到您有一行@bookmark = @user.bookmarks
。请注意,@bookmark
是单数。然后在视图模板中尝试运行@bookmarks.each
(复数=一个完全不相关的实例变量名称)。如果代码无法识别名称@bookmarks
,则该对象将默认为nil
,但不理解方法.each
。仔细检查你的变量名!这是一个常见的错误来源,这个特殊的错误(“Nil :: NilClass上的未知方法[某些方法名称”])在Rails开发中非常常见。
更一般地说,App.rb
文件的格式对我来说很奇怪。它看起来像是routes.rb
(将每个URL路由连接到控制器操作)和控制器(它接受请求输入并准备视图正确呈现所需的所有变量)的角色。您的应用程序是否有这样的结构?如果没有,我强烈建议您熟悉Rails的路由和标准结构。控制器;这两个代码通常保存在不同的地方是有充分理由的。 (除此之外,它可能会使这样的错误更容易排除故障!)