开发者

Ruby on Rails -- not grokking

I've been learning a lot about web technologies recently and I want to put together a neat little website to play with html, css, javascript, flash, and what have you.

I know that as far as the web browser is concerned all I need to do is send a text file (usually HTML) from my server using HTTP over TCP.

I've gone through a couple of Rails tutorials by now but I am having a lot of trouble getting things to work and understanding how the components work together even if I could get it to work by blindly following said tutorials to the letter.

I think I understand the MVC concept. Obviously the business model of an application makes sense to be kept separate from the view implementation, etc. Sure, that's fine. From what I understand RoR needs (?) a database. Sure, when my website grows to a point where I need to track customers and crunch data, I will want that.

But where do I get at the actual functionality? Where do I specify how my server responds to requests from browsers? Shouldn't this be straightforward? Do I need to set up a database just to get my "hello world" page up?

I think what might be happening is that Rails is designed to do something that I don't need (yet?). It does many things for me that I don't understand, and in order for me to be comfortable I would have to dig through a lot of material to figure it out.

What brought me to this point is, I am looking for the "next step" after this little server I was playing with:

require 'socket'     
server = TCPServer.open(8080)  
loop {
  Thread.start(server.accept) do |client|
    client.puts(Time.now.ctime) # server sends dynamic page consisting of current time
    client.flush
    while (str = client.gets) do 
      puts "recvd >> "+str # show what server gets from client
      if str.chop.length == 0 then break end # detect end of transmiss开发者_运维问答ion
    end 
    puts "done"
    client.close 
  end
}

This little bit of code is more than halfway there to what I need it to do. I got stuck on something pretty silly. I still don't know how to just take a file on disk and send it to the client. I figure i'd open a stream to the requested file and pipe it over to the socket... No idea how to do that in ruby. So I figured, hey, maybe Rails'll do it for me.

Now I follow a bunch of directions, finally get the server running on mongrel, and I try this: ruby script\generate controller MyTest and still get a "Routing Error" when I open it up in the browser. I get a giant ugly stack trace on the cmdline. Screw this!

Clearly I should be taking a class that will show me how to use Rails and more importantly, whether or not it is overkill for my purposes. So... should I keep trying to figure it out? If so, can someone show me a good tutorial, or explain to me why the tutorials I have seen aren't helping me?

Should I just try to use EventMachine to make my server? I just want to make a nice simple fast web server.

Thanks for reading. :)


You are way, way over-thinking things. Not that that's bad at all - it's great you want to learn the innards of how everything fits together and works, but Rails handles the vast majority of stuff like this for you.

For development purposes you are most likely going to use the Ruby webrick "server", which you can run from your applications home directory by doing: ruby script/server

Now, whenever you go to your application, which is by default going to be running at: http://localhost:3000/ , this server will process your requests and return the html for the appropriate view. If you have your app created and have a database set up, going to the url I mentioned will show you a "welcome aboard" message from Rails, letting you know everything is up and running. Later on when you push your app into production you'll be using the Apache server, but still, you won't have to worry about the things you mentioned in your question.

It is entirely possible to use Rails without a database, check out config/environment.rb:

# Skip frameworks you're not going to use. To use Rails without a database,
# you must remove the Active Record framework.
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

Just uncomment that last line and make sure you leave active_record in it.

I think what is going to help you best overcome your confusion is to research how Rails routing works, which is controlled by the config/routes.rb file. I think this will help you understand how Rails accepts and reacts to incoming requests.

And Rails is actually perfect for building little sandbox websites to play around with - once you get over the initial learning curve you can get stuff up and running very fast.


Your time server in Rails:

# app/controllers/time_controller.rb
# after ./script/server, available at:
# http://localhost:3000/time/
class TimeController < ApplicationController
  def index
    render :text => Time.now.ctime
  end
end

Database not needed. (You do need to remove ActiveRecord from the frameworks stack as Zachary shows in his answer).


Rails is not appropriate for this. You might be better off using apache for a static file server. Or, you could rewrite your socket server like this:

require 'socket'
server = TCPServer.open(8080)  
loop {
  Thread.start(server.accept) do |client|
    client.puts(Time.now.ctime) # server sends dynamic page consisting of current time
    client.flush
    while (str = client.gets) do 
      puts "recvd >> "+str # show what server gets from client
      if str.chop.length == 0
        client.write File.read(the_file_to_send)
        break
      end
    end 
    puts "done"
    client.close 
  end
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜