【问题标题】:Modular Sinatra - routes not working - structuring config.ru and multiple controllers to handle routes模块化 Sinatra - 路由不起作用 - 构建 config.ru 和多个控制器来处理路由
【发布时间】:2016-07-27 21:28:54
【问题描述】:

我正在按照本教程在 Sinatra::Base 上找到的食谱进行操作 https://www.safaribooksonline.com/library/view/sinatra-up-and/9781449306847/ch04.html

我在让我的路线工作时遇到问题,目前只有一条路线可以工作,那就是get '/',它从ApplicationController < Sinatra::Base home.erb 加载。

我的第二个控制器中名为 ExampleController < ApplicationController 的路由不起作用

config.ru(仅相关代码)

# Load Controllers and models
Dir.glob('./{controllers,models}/*.rb').each { |file| require file }

map('/example') { run ExampleController }
map('/') { run ApplicationController }

application_controller.rb

class ApplicationController < Sinatra::Base
  # set folder for root
  set :root, File.expand_path("../app", Dir.pwd)

  # don't enable logging when running tests
  configure :production, :development do
    enable :logging
  end

  get '/' do
    title "Home.erb"
    erb :home
  end

  not_found do
    title 'Not Found!'
    erb :not_found
  end

end

example_controller.rb当前不会加载路由

class ExampleController < ApplicationController

  get '/example' do
    title "Example Page"
    erb :example
  end

end

【问题讨论】:

    标签: ruby sinatra


    【解决方案1】:

    根据本教程,您似乎在 config.ru 文件和 ExampleController 的路由中都使用了路由 /example。这可能意味着您的本地 url 可能以 'http://localhost:4567/example/example' 结尾。从外观上看,您的 example_controller.rb 文件应该如下所示,路径为“/”:

    example_controller.rb

    class ExampleController < ApplicationController
    
      get '/' do
        title "Example Page"
        erb :example
      end
    
    end
    

    您似乎还需要在 config.ru 文件中使用 require 'sinatra/base'

    config.ru

    # Load Controllers and models
    require 'sinatra/base'
    Dir.glob('./{helpers,controllers}/*.rb').each { |file| require file }
    
    map('/example') { run ExampleController }
    map('/') { run ApplicationController }
    

    此外,您的 application_controller.rb 似乎缺少您的助手,并且不包括设置视图。

    application_controller.rb

    class ApplicationController < Sinatra::Base
      helpers ApplicationHelper
    
      # set folder for templates to ../views, but make the path absolute
      set :views, File.expand_path('../../views', __FILE__)
    
      # don't enable logging when running tests
      configure :production, :development do
        enable :logging
      end
    
      get '/' do
        title "Home.erb"
        erb :home
      end
    
      # will be used to display 404 error pages
      not_found do
        title 'Not Found!'
        erb :not_found
      end
    end
    

    【讨论】:

    • 不幸的是它仍然抛出一个错误,WEBrick 说"GET /example HTTP/1.1" 404 553 0.0030
    • 你重启服务器了吗?
    • 我注意到您缺少与本教程相关的另一点信息。我已经更新了上面的答案。您是否需要在{controllers,models} 中包含您的助手? (即{helpers,controllers,models}
    • 嗨,我已经成功地模拟了本教程并让它发挥作用。教程中有一些错别字,从外观上看,您缺少很多信息。已在上面更新了我的答案,但如果它仍然不起作用,我可以将我所做的推送到我的 GitHub 存储库,欢迎您将其拉出并比较脚本以查看哪里出错了。 :-)
    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多