2

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.

6
  • @Casper Thanks, Actually I'm new there so don't know about that. Commented Dec 11, 2012 at 12:59
  • 1
    Ok Tarun. Ruby is not a "web page creation language". As such you will not see it embedded directly into web pages like PHP. Getting it up and running with a web server is a bit more complex than with PHP. You might want to look at this question for some further direction: stackoverflow.com/questions/5664553/… . Also an even simpler approach: coolnamehere.com/geekery/ruby/web/cgi.html Commented Dec 11, 2012 at 13:15
  • The reason you're not finding any easy way of doing this is that it isn't easy. Ruby without a framework isn't made for the web the way PHP is made specifically for the web. What you're asking is sort of like asking how to use PHP without any Apache modules. Commented Dec 11, 2012 at 13:46
  • 1
    "it isn't easy"? I disagree, it's very easy, if you know what is needed. Unfortunately, people don't learn the CGI basics so everything is a mystery, but once that's mastered writing code for the web is easy in shell, Ruby, Perl, C, etc. It's learning the frameworks out there that is hard. Commented Dec 11, 2012 at 15:37
  • @theTinMan - Everything is easy when you know how. It's the knowing that is the hard part :) Commented Dec 11, 2012 at 16:57

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to explain it by better way and to share awesome article.
1

Something like this should do it:

http://www.editrocket.com/articles/ruby_apache_windows.html

3 Comments

Thanks it's very useful, But i read somewhere that we have to use rack to support all web servers like webrick etc. So I'm looking something like index, about, contact, or may be connection with database support in all web server. Something like php code(portable)
@Tarun You've fundamentally misunderstood how Ruby works. You need to use Ruby on Rails or another framework for what you're asking for.
Well you could technically run some stand alone ruby without a framework, not very practical in real world but might be useful for learning some simple ruby. Most likely ruby on rails is the way to go though in this instance.
1

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?

1 Comment

Thanks buddy, Actually I'm not able to see video now. But as per your reply it's seems you got what I'm looking for :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.