【问题标题】:Sinatra App - Separating ConcernsSinatra 应用程序 - 分离关注点
【发布时间】:2015-11-10 21:59:52
【问题描述】:

可能是一些非常基本的东西,但我希望能够将我的 Sinatra 路由与控制器分开。我的routes.rb 中有这段代码:

require 'sinatra/base'

class Server < Sinatra::Base
  get '/' do
   Action.index
  end
end

这是我的controller/server.rb

class Action
  def sef.index
     @user = User.new("Abiodun Shuaib")
     haml: index
  end
end

它给出了错误undefined method 'haml' in Action:Class

我该如何解决这个问题?

【问题讨论】:

    标签: ruby rubygems sinatra haml


    【解决方案1】:

    您正在尝试访问类 Action 中的方法 haml。它根本不包含它。 例如,您可以这样做:

    class Server
      def index
        @user = User.new("Abiodun Shuaib")
        haml :index
      end
    end
    

    通过这样做,您将添加到服务器方法索引。

    或者你可以这样做(它被称为 Mixin):

    module ActionNew
      def index
        @user = User.new("Abiodun Shuaib")
        haml :index
      end
    end
    
    class Server < Sinatra::Base
      include ActionNew
      get '/' do
        index
      end
    end 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      相关资源
      最近更新 更多