问题
I'm trying to learn ruby for web from scratch. I know PHP and I'm new in ruby. I don't want to use any framework at beginning. Trying to create simple 2 - 3 pages for practice. I search a lot but not got any example to create webpage from ruby and racks and to run it in any browser.
Guys please share if you do have any URL or online material to get start.
回答1:
Writing a simple CGI in Ruby is easy, however, explaining how is beyond the scope of Stack Overflow.
To start, read Wikipedia's Common Gateway Interface article so you have a basic idea of what a CGI has to return. Pick apart the Perl example; You should be able to figure out what's going on easily enough. Read through the article and compare what it says to what the code example is doing.
Next, look at Ruby's CGI module, because it will help you parse incoming form information, and encode/decode URLs and HTML. Look at the table of environment variables, and you'll see the same things passed in that are mentioned in the Wikipedia article. Next, notice the params()
method, which is how you access form variables. Follow that by reading the "Writing output" and "Generating HTML" sections, and nose about in the examples. You should be on your way then.
Once you've done that, you should take a look at the Sinatra gem. It's a very nice, easy to use, framework that is well suited for general web use, and, after getting an understanding of how a CGI works, you'll be in a good place to appreciate what Sinatra does for you. Additionally, I highly recommend using HAML with Sinatra. It's a short-cut language for generating web pages, similar to ERB, but less verbose.
You'll find that PHP has shielded you from a lot of the knowledge needed to write a "normal" web application using a CGI. That's OK if you already know that stuff, but it's bad if you have never done lower-level code, or are trying to use different languages, because knowing the CGI layer provides a lot of knowledge that is needed to understand the whole HTTPD stack. Once you know the CGI layer, things like Sinatra, Rails, Django, Mojolicious and all the other frameworks out there suddenly make more sense, and you'll know when to take advantage of them or roll your own. And, PHP's place in the HTTP server will be clearer too, allowing you to pick and choose your tools better for the job at hand.
回答2:
Something like this should do it:
http://www.editrocket.com/articles/ruby_apache_windows.html
回答3:
Check out Ben Scofield's talk from RubyConf 2008 - Building my own Web Framework Should give you some idea about what is involved in building a web app with ruby from scratch without using any frameworks.
The answers to this previous question should be helpful as well: Complete web apps in Ruby without using any frameworks - how difficult? Pitfalls?
来源:https://stackoverflow.com/questions/13820476/ruby-web-pages-without-any-framework-from-scratch