Ruby & 入门Ruby on Rails [英] Getting Started with Ruby & Ruby on Rails

查看:52
本文介绍了Ruby & 入门Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:

我是一个多面手,其中之一就是编程.我通过 Excel 和 PHP 学习了 VB6 来创建网站,到目前为止它对我来说效果很好.我不是计算机科学专业,甚至不擅长数学 - 我感兴趣的是逻辑.

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.

当前状态:

我愿意学习新的、更强大的语言;我第一次尝试这样的路线是学习 Ruby.我去了主要的 Ruby 网站 并做了交互式介绍.(顺便说一下,当我尝试链接时,我目前被重定向到 google.com...其他网站也发生这种情况...我的计算机是否被感染?)

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?)

我喜欢我学到的东西,并想开始使用 Ruby 来创建网站.我下载了 InstantRails 并安装了它;到目前为止一切都很好——程序启动得很好,我可以在控制台中测试一些 Ruby 代码.然而,当我尝试查看带有 Ruby 代码的网页时,我的麻烦就开始了.

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.

最后,我的问题:

与在 PHP 中一样,我可以直接浏览到 .php 文件,也可以通过使用 PHP 标签和一些简单的回显"语句来制作动态网页.但是,在 InstantRails 应用程序工作时,访问 .rb 或 .rhtml 页面不会产生类似的结果.我制作了一个名为test.rb"的简单文本文件,并在其中放置了基本的 HTML 标签(html、head、body)和 Ruby 标签 <%= 和 %> 以及一些 ruby​​ 代码.网页实际上显示了标签和代码——就好像它只是普通的 HTML.我认为 Ruby 不会在页面显示给用户之前对其进行解析,但这就是我对 Ruby 环境缺乏了解的地方.我从这里去哪里?

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.

推荐答案

首先,您必须断开文件和 URL 之间的关系.Rails 使用 MVC 方法,这与 ASP/PHP 等基于脚本的方法截然不同

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

在经典的 PHP 中,你有这样的东西

In classic PHP, you have something like this

  1. Server> 服务器启动,从 /usr/jake/example.com/htdocs/
  2. 提供脚本
  3. 用户>请给我/home.php,谢谢!
  4. Server> OK,/home.php 映射到 /usr/jake/example.com/htdocs/home.php
  5. Server> 执行 /usr/jake/example.com/htdocs/home.php
  6. Server> 好的,它打印出Hello World!",将其发送到响应中.
  7. User> 好的,/home.php 显示Hello World!"
  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!"

然而,大多数 MVC 框架(包括 Rails)都是这样的:

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

  1. Server> 服务器启动,初始化路由模块routes.rb
  2. 用户>请给我/home,谢谢!
  3. Server> OK,/home,根据路由模块,由控制器FrontpageCtr
  4. 中的操作ShowHomepage()处理
  5. Server> 执行 FrontPageCtr.ShowHomepage()
  6. Ruby> FrontPageCtr.ShowHomepage() 打印Hello World!"
  7. Server> OK,发送Hello World!"下管道!
  8. User> 好的,/home 显示Hello World!"
  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!"

如您所见,用户放入地址栏中的内容与任何脚本文件之间没有联系

在典型的 MVC 框架中,处理对任何 URL 的请求是这样的:

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

  1. 查看路由模块(在 Rails 的情况下,它在 routes.rb 中定义)
  2. 然后路由模块会告诉服务器应该使用哪个控制器"和操作"来处理请求.
  3. Rails 然后创建控制器并调用任何可能的 Action 函数
  4. 动作的结果然后被渲染",在这种情况下,它应该将 .rhtml 文件渲染为实际的 HTML……当然,还有其他类型的结果例如将用户发送到另一个 URL 等等.
  5. 然后将结果写出到响应流并由用户的浏览器显示.
  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.

<小时>

简而言之:您必须首先断开脚本和 URL 的概念.当您构建 MVC 网站时,它们几乎总是以大多数人理解的方式相关.


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.

考虑到这一点,您应该更轻松地学习 Rails 和 MVC 的生活方式.

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

我不是 Rails 专家,所以如果我有任何错误,请纠正我.

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

这篇关于Ruby &amp; 入门Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆