views:

19

answers:

1

Hello guys ! 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.

A: 

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.

ismaelsow