开发者

Getting Started with Ruby & Ruby on Rails

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开发者_如何学Go 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.


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.


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.


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


Getting started with Ruby on Rails is something that is a little daunting at first, but after you get started it gets a lot easier. After running Ruby on Rails bootcamps for Startup Accelerators, Harvard Business School, in Times Square, Boston, and Pittsburgh, I started http://www.firehoseonline.com. It's a video tutorial to get started, so you should check out that site.

My advice is to learn as much as you can by actually writing the code. Don't get caught up too much in the details and the specifics. If a tutorial gives you some code to write, and some information, and you don't absorb all the information at first, keep going. Afterwards go back to the material, and once you have gone through the whole process of writing your first application a lot of the pieces will fit together.

As far as your question about opening the php files directly, using the MVC pattern is a little different. You need to setup a the controller, the views and the routes before you can start putting code into .rhtml (or now .html.erb) files. Because of this architecture you'll be able to write a lot of awesome, clean code, super fast, but it can be a bit tricky to wrap your head around (if you REALLY want to write code that way you can with other frameworks, but trust us that this way is better). Stick with it!

Keep your coding mojo high!

Aloha, Ken

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜