#<nomethoderror:undefined =“”method =“”`any?'=“”for =“”nil:nilclass =“”>

时间:2016-07-18 14:12:04

标签: ruby sinatra datamapper

我正在尝试使用带有多个数据模型的datamapper构建一个简单的应用程序。有一段时间,我工作的是我有一个父母和一个儿童模型相关联。但是,我搞砸了一些东西,无法让我的两个模型中的一个工作。我将使用简化的代码来尝试查看第二个模型(称为Songs)是否正常工作。名为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(Slim):

.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"

宝石文件:

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'

3 个答案:

答案 0 :(得分:1)

出乎我的意料,你可能没有任何歌曲。您无法在nil上调用!@songs.to_s.empty?,从而导致此错误。确保你有歌曲,然后再试一次。

要避免出现此错误,请使用var callApi = function(url, data) { $http({ method: "POST", url: url, data: JSON.stringify(data), contentType:"application/json" }).then( function (result) { alert(result.data); }, function (error) { alert(error.status); } ); } 或确保它返回[]而不是nil

答案 1 :(得分:0)

Maxim是对的。

你可以按照自己的方式行事:

结合检查:

if @songs && @songs.any?

在控制器调用中初始化@songs,因此它不是nil,而是一个空数组:

@songs = []

答案 2 :(得分:0)

使用

进行检查
@songs.present?

要详细了解any?, present?, empty?, blank?,请查看此link