#<NoMethodError: undefined method `any?' for nil:NilClass>
#<NoMethodError: undefined method `any?' for nil:NilClass>
我正在尝试使用具有多个数据模型的数据映射器构建一个简单的应用程序。有一次,我工作的是我有一个关联的父模型和一个子模型。但是,我搞砸了,无法让我的两个模型中的一个工作。我将使用我正在使用的简化代码来尝试查看第二个模型(称为歌曲)是否正常工作。名为 Soundtrack 的模型的路线有效。
第一次发帖,如有不足请见谅。请告诉我,我会添加更多内容。
乔恩
模型和路线:
require 'data_mapper'
require 'dm-timestamps'
#models
configure :development do
DataMapper.setup(:default, "sqlite3://#{ Dir.pwd}/development.db")
end
configure :production do
DataMapper.setup(:default, ENV['DATABASE_URL'])
end
class Soundtrack
include DataMapper::Resource
property :id, Serial
property :genre, String
property :tagline, String
property :created_at, DateTime
property :title, String
property :createdby, String
end
class Songs
include DataMapper::Resource
property :id, Serial
property :songtitle, String
property :artist, String
property :scene, String
end
DataMapper.finalize
configure :development do
DataMapper.auto_migrate!
end
#Routes
get '/soundtrack' do
@soundtrack = Soundtrack.all
slim :soundtrack
end
get '/singletrack' do
@songs = Songs.all
slim :singletrack
end
get '/soundtrack/new' do
@soundtrack = Soundtrack.new
slim :new_soundtrack
end
get '/soundtrack/:id' do
@soundtrack = Soundtrack.get(params[:id])
slim :show_soundtrack
end
get '/soundtrack/:id/edit' do
@soundtrack = Soundtrack.get(params[:id])
slim :edit_soundtrack
end
post '/soundtrack' do
@soundtrack = Soundtrack.create(params[:soundtrack])
redirect to("/soundtrack/#{@soundtrack.id}")
end
post '/soundtrack/:id' do
@soundtrack = Soundtrack.get(params[:id]).songinfo.create params['songinfo']
redirect back
end
post '/songs/:id' do
@songs = Songs.create(params[:songs])
redirect back
end
put '/soundtrack/:id' do
soundtrack = Soundtrack.get(params[:id])
soundtrack.update(params[:soundtrack])
redirect to('/soundtrack/[:id]')
end
delete '/soundtrack/:id' do
Soundtrack.get(params[:id]).destroy
redirect to('/soundtrack')
end
delete '/songinfo/:id' do
Songinfo.get(params[:id]).destroy
redirect back
end
HTML(苗条):
.frame
.row
<div class="table-responsive">
<table class="table">
thead.background-black
<tr>
th.text-title Song
th.text-title Artist
th.text-title Scene
</tr>
</thead>
<tbody>
- if @songs.any?
tr#songs
-@songs.each do |songs|
tr
th #{songs.songtitle}
th #{soundtrack.artist}
th #{soundtrack.scene}
</tr>
- else
tr
th No songs have been created yet!
</tbody>
</table>
</div>
.col-md-2
.col-md-10
pre.pre-black
row
form-horizontal
form-group
col-sm-2
label for="Song Title"
p.text-title.form-lower Soundtrack Title
col-sm-2
input.form-control type="string" name="songs[songtitle]" value="#{@songs.songtitle}"
row
form-horizontal
form-group
col-sm-2
label for="Song Artist"
p.text-title.form-lower Song Artist
col-sm-10
input.form-control type="string" name="songs[artist]" value="#{@songs.artist}"
row
form-horizontal
form-group
col-sm-2
label for="Scene"
p.text-title.form-lower Scene in Moveie
col-sm-10
input.form-control type="string" name="songs[scene]" value="#{@songs.scene}"
.col-md-6
h3.bump class==current?("/songs") Add A New Song
.form method="POST" action="/songs"
input.lower.link-size type="submit" class="text-center" value="Save Song"
Gem 文件:
source :rubygems
gem "sinatra"
gem "slim"
gem "data_mapper"
gem "dm-timestamps"
gem "thin"
gem "dm-postgres-adapter", :group => :production
gem "dm-sqlite-adapter", :group => :development
gem 'pg', '~> 0.18.4'
我没想到,你可能没有任何歌曲。您不能在 nil 上调用 .any?
,这会导致此错误。请确保您有歌曲,然后重试。
并且为了在没有 nil 的情况下避免此错误,请使用 !@songs.to_s.empty?
或确保 returns 一个 [] 而不是 nil
格言是对的。
你可以按他的方式做,或者:
结合检查:
if @songs && @songs.any?
在控制器调用中初始化@songs,所以它不是nil,而是一个空数组:
@songs = []
检查使用
@songs.present?
要了解有关 any?, present?, empty?, blank?
的更多信息,请查看此 link。
我正在尝试使用具有多个数据模型的数据映射器构建一个简单的应用程序。有一次,我工作的是我有一个关联的父模型和一个子模型。但是,我搞砸了,无法让我的两个模型中的一个工作。我将使用我正在使用的简化代码来尝试查看第二个模型(称为歌曲)是否正常工作。名为 Soundtrack 的模型的路线有效。
第一次发帖,如有不足请见谅。请告诉我,我会添加更多内容。
乔恩
模型和路线:
require 'data_mapper'
require 'dm-timestamps'
#models
configure :development do
DataMapper.setup(:default, "sqlite3://#{ Dir.pwd}/development.db")
end
configure :production do
DataMapper.setup(:default, ENV['DATABASE_URL'])
end
class Soundtrack
include DataMapper::Resource
property :id, Serial
property :genre, String
property :tagline, String
property :created_at, DateTime
property :title, String
property :createdby, String
end
class Songs
include DataMapper::Resource
property :id, Serial
property :songtitle, String
property :artist, String
property :scene, String
end
DataMapper.finalize
configure :development do
DataMapper.auto_migrate!
end
#Routes
get '/soundtrack' do
@soundtrack = Soundtrack.all
slim :soundtrack
end
get '/singletrack' do
@songs = Songs.all
slim :singletrack
end
get '/soundtrack/new' do
@soundtrack = Soundtrack.new
slim :new_soundtrack
end
get '/soundtrack/:id' do
@soundtrack = Soundtrack.get(params[:id])
slim :show_soundtrack
end
get '/soundtrack/:id/edit' do
@soundtrack = Soundtrack.get(params[:id])
slim :edit_soundtrack
end
post '/soundtrack' do
@soundtrack = Soundtrack.create(params[:soundtrack])
redirect to("/soundtrack/#{@soundtrack.id}")
end
post '/soundtrack/:id' do
@soundtrack = Soundtrack.get(params[:id]).songinfo.create params['songinfo']
redirect back
end
post '/songs/:id' do
@songs = Songs.create(params[:songs])
redirect back
end
put '/soundtrack/:id' do
soundtrack = Soundtrack.get(params[:id])
soundtrack.update(params[:soundtrack])
redirect to('/soundtrack/[:id]')
end
delete '/soundtrack/:id' do
Soundtrack.get(params[:id]).destroy
redirect to('/soundtrack')
end
delete '/songinfo/:id' do
Songinfo.get(params[:id]).destroy
redirect back
end
HTML(苗条):
.frame
.row
<div class="table-responsive">
<table class="table">
thead.background-black
<tr>
th.text-title Song
th.text-title Artist
th.text-title Scene
</tr>
</thead>
<tbody>
- if @songs.any?
tr#songs
-@songs.each do |songs|
tr
th #{songs.songtitle}
th #{soundtrack.artist}
th #{soundtrack.scene}
</tr>
- else
tr
th No songs have been created yet!
</tbody>
</table>
</div>
.col-md-2
.col-md-10
pre.pre-black
row
form-horizontal
form-group
col-sm-2
label for="Song Title"
p.text-title.form-lower Soundtrack Title
col-sm-2
input.form-control type="string" name="songs[songtitle]" value="#{@songs.songtitle}"
row
form-horizontal
form-group
col-sm-2
label for="Song Artist"
p.text-title.form-lower Song Artist
col-sm-10
input.form-control type="string" name="songs[artist]" value="#{@songs.artist}"
row
form-horizontal
form-group
col-sm-2
label for="Scene"
p.text-title.form-lower Scene in Moveie
col-sm-10
input.form-control type="string" name="songs[scene]" value="#{@songs.scene}"
.col-md-6
h3.bump class==current?("/songs") Add A New Song
.form method="POST" action="/songs"
input.lower.link-size type="submit" class="text-center" value="Save Song"
Gem 文件:
source :rubygems
gem "sinatra"
gem "slim"
gem "data_mapper"
gem "dm-timestamps"
gem "thin"
gem "dm-postgres-adapter", :group => :production
gem "dm-sqlite-adapter", :group => :development
gem 'pg', '~> 0.18.4'
我没想到,你可能没有任何歌曲。您不能在 nil 上调用 .any?
,这会导致此错误。请确保您有歌曲,然后重试。
并且为了在没有 nil 的情况下避免此错误,请使用 !@songs.to_s.empty?
或确保 returns 一个 [] 而不是 nil
格言是对的。
你可以按他的方式做,或者:
结合检查:
if @songs && @songs.any?
在控制器调用中初始化@songs,所以它不是nil,而是一个空数组:
@songs = []
检查使用
@songs.present?
要了解有关 any?, present?, empty?, blank?
的更多信息,请查看此 link。