Sinatra范围nomethoderror

时间:2017-04-03 04:27:47

标签: ruby-on-rails ruby scope sinatra

我正在尝试制作Sinatra应用程序。

但现在我有一个我无法解决的错误。所以我想要帮助。

当我输入/ tasks?status = done时,应用程序会发出警告

  

/ tasks undefined方法中的NoMethodError`status_is_done'

如何修复错误?如果可以,请告诉我任何提示。

以下是型号代码。

module Todo
class Task < ActiveRecord::Base
scope :status_is, ->(status){where(status: status)}

scope :status_is_not_yet, ->{status_is(NOT_YET)}
scope :status_is_done, ->{status_is(DONE)}
scope :status_is_pending, ->{status_is(PENDING)}

NOT_YET = 0
DONE = 1
PENDING = 2

STATUS = {
    'NOT_YET' => NOT_YET,
    'DONE' => DONE,
    'PENDING' => PENDING
}.freeze

validates :name, presence: true, length: {maximum: 140}
validates :content, presence: true
validates :status, numericality: true, inclusion: {in: STATUS.values}


def status_name
  STATUS.key(self.status)
end

端 端

这是控制器代码。

require 'sinatra/base'
require 'haml'


require 'todo/db'
require 'todo/task'

module Todo
class Application < Sinatra::Base

set :haml, escape_html: true

configure do
  DB.prepare
end

configure :development do
  require 'sinatra/reloader'
  register Sinatra::Reloader
end

get '/' do
  redirect :tasks
end

get '/tasks' do
  @tasks = Task.order('created_at DESC')
  #statusによって@statusの値を変更
  if (@status = params[:status])
    case @status
    when 'not_yet'
      @tasks = @tasks.status_is_not_yet
    when 'done'
      @tasks = @tasks.status_is_done
    when 'pending'
      @tasks = @tasks.status_is_pending
    else
      @status = nil
    end
  end

  haml :index
end

end
end

1 个答案:

答案 0 :(得分:0)

尝试替换

@tasks = @tasks.status_is_done

@tasks = Task.order('created_at DESC').status_is_done