views:

75

answers:

3

Some background:

I'm a jack-of-all trades, one of which is programming. I learned VB6 through Excel and PHP for creating websites and so far it's worked out just fine for me. I'm not CS major or even mathematically inclined - logic is what interests me.

Current status:

I'm willing to learn new and more powerful languages; my first foray into such a route is learning Ruby. I went to the main Ruby website and did the interactive intro. (by the way, I'm currently getting redirected to google.com when I try the link...it's happening to other websites as well...is my computer infected?)

I liked what I learned and wanted to get started using Ruby to create websites. I downloaded InstantRails and installed it; everything so far has been fine - the program starts up just fine, and I can test some Ruby code in the console. However my troubles begin when I try and view a web page with Ruby code present.

Lastly, my problem:

As in PHP, I can browse to the .php file directly and through using PHP tags and some simple 'echo' statements I can be on my way in making dynamic web pages. However with the InstantRails app working, accessing a .rb or .rhtml page doesn't produce similar results. I made a simple text file named 'test.rb' and put basic HTML tags in there (html, head, body) and the Ruby tags <%= and %> with some ruby code inside. The web page actually shows the tags and the code - as if it's all just plain HTML. I take it Ruby isn't parsing the page before it is displayed to the user, but this is where my lack of understanding of the Ruby environment stops me short. Where do I go from here?

AMMENDMENT: This tutorial has helped me immensely! I'd suggest anyone who's in my position go there.

+3  A: 

First of all, you must disconnect the relationship between files and URLs. Rails uses an MVC approach, which is worlds-different from scripts-based approach like ASP/PHP

In classic PHP, you have something like this

  1. Server> Server started, serving scripts from /usr/jake/example.com/htdocs/
  2. User> Please give me /home.php, thanks!
  3. Server> OK, /home.php is mapped to /usr/jake/example.com/htdocs/home.php
  4. Server> Executing /usr/jake/example.com/htdocs/home.php
  5. Server> OK, it prints out a "Hello World!", send that to the response.
  6. User> Ok, /home.php shows "Hello World!"

However, most MVC framework (Rails included) goes something like this:

  1. Server> Server started, initializing routing modules routes.rb
  2. User> Please give me /home, thanks!
  3. Server> OK, /home, per the routing module, is handled with action ShowHomepage() in controller FrontpageCtr
  4. Server> Execute FrontPageCtr.ShowHomepage()
  5. Ruby> FrontPageCtr.ShowHomepage() prints "Hello World!"
  6. Server> OK, sending "Hello World!" down the pipes!
  7. User> Ok, /home shows "Hello World!"

As you can see, there is no connection between what the user put into the addressbar and any script files

In a typical MVC framework, processing a request for any URL goes something like this:

  1. Look in the Routing module (which in the case of rails is defined in routes.rb)
  2. Routing module will then tells the server which "Controller" and "Action" should be used to handle the request.
  3. Rails then creates the Controller and invokes the Action function whatever that might be
  4. The result from the action then gets "Rendered", which, in this case, is supposedly rendering the .rhtml file as actual HTML... there are, of course, other kinds of results e.g. send the user to another URL and whatnot.
  5. The result is then written out to the response stream and displayed by the user's browser.

In short: You must disconnect the notion of scripts and URL first. When you're building MVC websites, they are almost always NOT related in a way that most people understand.

With that in mind, you should be more comfortable learning Rails and MVC way of life.

I'm not a Rails pro so please correct me if I'm mistaken on any part.

chakrit
+1  A: 

I would suggest buying and working your way through Agile Web Development with Rails, an excellent book and a very practical way to learn both Ruby and Rails. It's available instantly in a variety of electronic formats, plus you can get paper copy if you prefer that.

From what you describe you have a fundamentally flawed understanding of how Ruby and Rails, in particular, works. I suggest you spend some time with the book then come back and ask about anything that you get stumped on.

tvanfosson
A: 

Rails is "parsing the page before it is displayed to the user", if you locate the right file to modify ;-) Those files to be modified are under the following folder(s):

app/views/...

That's the short answer. For a comprehensive one (for a newbie), I highly recommend: http://guides.rubyonrails.org/getting_started.html

ohho