杂种问题:相同的网址,不同的响应

发布于 2024-09-09 01:57:04 字数 2204 浏览 5 评论 0原文

我正在编写一个小应用程序,以便总体上学习 Ruby 和 Web 开发。该应用程序是一个小博客,网络服务器是杂种。我在 mongrel 之上建立了一个简单的 MVC 结构,带有前端控制器和 url 调度程序。当我第一次访问网址 http://myapp/article/show/hello 时,显示“hello”文章的内容。

但从那时起,当我刷新页面或转到另一个网址时,我看到的就是我设置的 404 错误页面。我发现解决问题的唯一方法是为每个请求重新启动服务器,这显然不是解决方案!

我的代码如下所示:

launch.rb

require 'rubygems'
require 'mongrel'
require 'config/settings'
require File.join(Settings::UTILS_DIR, "dispatcher.rb") 

class FrontController < Mongrel::HttpHandler
  def process(request, response)

    route = Dispatcher.new.dispatch(request.params["REQUEST_URI"]) 
    controller = route["class"] 
    action = route["method"]
    params = route["params"]
    action_output = controller.new.send(action, params)

    response.start(200) do |head, out|
      head["Content-Type"] = "text/html" 
      out << action_output 
    end 
  end 
end

h = Mongrel::HttpServer.new("192.168.0.103", "3000") 
h.register("/", FrontController.new)
h.run.join

Dispatcher.rb

require File.join(Settings::CONFIG_DIR, "controllers.rb")

class Dispatcher
  def dispatch(uri)

    uri = uri.split("/")
    if uri.size == 0
      return {"class" => Controllers.const_get(:ArticleActions), "method" => :index, "params" => nil}
    end 

    route = {}
    unless uri[1].nil? and uri[2].nil?
      controller_class = uri[1].capitalize << "Actions" # All controllers classes follow this form : ControllerActions
      controller_method = uri[2]
      route["class"] = Controllers.const_defined?(controller_class) ? Controllers.const_get(controller_class) : nil 
      route["method"] = ( route["class"] and route["class"].method_defined?(controller_method) ) ? controller_method : nil 
      route["params"] = uri.slice(3, uri.size) ? uri.slice(3, uri.size) : nil 
    end 

    # If url not valid
    if route["class"].nil? or route["method"].nil?
      route["class"] = Controllers.const_get(:ErrorActions)
      route["method"] = :error404
      route["params"] =  nil 
    end 

    route
  end 
end

要启动应用程序和服务器,我只需执行 ruby​​ launch.rb 即可。 从昨晚开始,我的头就很痛。有什么想法吗? 谢谢。

I am coding a little app in order to learn ruby and web development in general. The app is a little blog and the web server is mongrel. I have set up a simple MVC structure on top of mongrel, with a front controller and a url dispatcher. When I go to the url http://myapp/article/show/hello for the first time, the content of "hello" article is displayed.

But from that point, when I refresh the page or go to another url, it's the 404 error page I have set up that I see. The only way I found to fix the problem is to restart the server for each request, wich clearly, is not a solution !

My code looks like this :

launch.rb

require 'rubygems'
require 'mongrel'
require 'config/settings'
require File.join(Settings::UTILS_DIR, "dispatcher.rb") 

class FrontController < Mongrel::HttpHandler
  def process(request, response)

    route = Dispatcher.new.dispatch(request.params["REQUEST_URI"]) 
    controller = route["class"] 
    action = route["method"]
    params = route["params"]
    action_output = controller.new.send(action, params)

    response.start(200) do |head, out|
      head["Content-Type"] = "text/html" 
      out << action_output 
    end 
  end 
end

h = Mongrel::HttpServer.new("192.168.0.103", "3000") 
h.register("/", FrontController.new)
h.run.join

dispatcher.rb

require File.join(Settings::CONFIG_DIR, "controllers.rb")

class Dispatcher
  def dispatch(uri)

    uri = uri.split("/")
    if uri.size == 0
      return {"class" => Controllers.const_get(:ArticleActions), "method" => :index, "params" => nil}
    end 

    route = {}
    unless uri[1].nil? and uri[2].nil?
      controller_class = uri[1].capitalize << "Actions" # All controllers classes follow this form : ControllerActions
      controller_method = uri[2]
      route["class"] = Controllers.const_defined?(controller_class) ? Controllers.const_get(controller_class) : nil 
      route["method"] = ( route["class"] and route["class"].method_defined?(controller_method) ) ? controller_method : nil 
      route["params"] = uri.slice(3, uri.size) ? uri.slice(3, uri.size) : nil 
    end 

    # If url not valid
    if route["class"].nil? or route["method"].nil?
      route["class"] = Controllers.const_get(:ErrorActions)
      route["method"] = :error404
      route["params"] =  nil 
    end 

    route
  end 
end

To launch the app and the server, I just do ruby launch.rb.
Since the last night, my head is hurting. Any idea ?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-09-16 01:57:04

也许我得到了提示。
这是我在控制台上调试会话的结果:

开始请求 /article/index
uri[0]: uri[1]: 文章 uri[2]: 索引 uri[3]:
控制器类:文章操作
控制器方法:索引
结束请求 /article/index

开始请求 /article/index
uri[0]: uri[1]: 文章 uri[2]: 索引 uri[3]:
控制器类:文章操作
控制器方法:索引
结束请求 /article/index

开始请求 /favicon.ico

uri[0]: uri[1]: favicon.ico uri[2]: uri[3]:
控制器类:Favicon.icoActions
控制器方法:
7 月 10 日星期六 17:20:21 +0200 2010:读取错误 #NameError:常量名称错误 Favicon.icoActions
/home/ismaelsow/blog/utils/dispatcher.rb:22:在“const_define”中?
...

我认为浏览器发起了对 favicon.ico 的 GET 请求,但找不到它,但我在应用程序的根目录中有一个 favicon.ico 。
我尝试过一些变体:

h.register("/favicon.ico", Mongrel::DirHandler.new(File.dirname(__FILE__)))
h.register("/", FrontController.new)

但它似乎不起作用。

Maybe I have got an hint.
This is the result of my debugging session on the console:

Begin request /article/index
uri[0]: uri[1]: article uri[2]: index uri[3]:
controller_class: ArticleActions
controller_method: index
End request /article/index

Begin request /article/index
uri[0]: uri[1]: article uri[2]: index uri[3]:
controller_class: ArticleActions
controller_method: index
End request /article/index

Begin request /favicon.ico

uri[0]: uri[1]: favicon.ico uri[2]: uri[3]:
controller_class: Favicon.icoActions
controller_method:
Sat Jul 10 17:20:21 +0200 2010: Read error #NameError: wrong constant name Favicon.icoActions
/home/ismaelsow/blog/utils/dispatcher.rb:22:in `const_defined?'
...

I think the browser initiates a GET request for favicon.ico and fails to find it, tough I have a favicon.ico in the root directory of the app.
I have tried this with some variations:

h.register("/favicon.ico", Mongrel::DirHandler.new(File.dirname(__FILE__)))
h.register("/", FrontController.new)

But it does not seem to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文