简体   繁体   English

Ruby和Ruby on Rails入门

[英]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. 我通过Excel和PHP学习了VB6来创建网站,到目前为止,对我来说还算不错。 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. 我第一次尝试这种方法是学习Ruby。 I went to the main Ruby website and did the interactive intro. 我去了主要的Ruby网站并进行了交互式介绍。 (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?) (顺便说一句,当我尝试链接时,我当前正被重定向到google.com。它也正在其他网站上发生...我的计算机是否受到感染?)

I liked what I learned and wanted to get started using Ruby to create websites. 我喜欢所学的知识,并希望开始使用Ruby创建网站。 I downloaded InstantRails and installed it; 我下载并安装了InstantRails; everything so far has been fine - the program starts up just fine, and I can test some Ruby code in the console. 到目前为止,一切都很好-程序启动正常,我可以在控制台中测试一些Ruby代码。 However my troubles begin when I try and view a web page with Ruby code present. 但是,当我尝试查看存在Ruby代码的网页时,我的麻烦就开始了。

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. 与在PHP中一样,我可以直接使用PHP标记和一些简单的“ echo”语句浏览到.php文件,从而可以制作动态网页。 However with the InstantRails app working, accessing a .rb or .rhtml page doesn't produce similar results. 但是,在InstantRails应用程序正常工作的情况下,访问.rb或.rhtml页面不会产生相似的结果。 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. 我制作了一个名为'test.rb'的简单文本文件,并在其中放置了基本HTML标记(html,head,body)和Ruby标记<%=和%>,并在其中添加了一些红宝石代码。 The web page actually shows the tags and the code - as if it's all just plain HTML. 该网页实际上显示了标记和代码-好像都是纯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. 我认为Ruby在将页面显示给用户之前不会解析页面,但这就是我对Ruby环境缺乏理解的原因。 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 . 首先,您必须断开文件和URL之间的关系 Rails uses an MVC approach, which is worlds-different from scripts-based approach like ASP/PHP Rails使用MVC方法,这与基于脚本的方法(例如ASP / PHP)有天壤之别

In classic PHP, you have something like this 在经典的PHP中,您有类似这样的内容

  1. Server> Server started, serving scripts from /usr/jake/example.com/htdocs/ 服务器>服务器已启动,从/usr/jake/example.com/htdocs/提供脚本
  2. User> Please give me /home.php , thanks! 用户>请给我/home.php ,谢谢!
  3. Server> OK, /home.php is mapped to /usr/jake/example.com/htdocs/home.php 服务器>确定,/ /home.php映射到/usr/jake/example.com/htdocs/home.php
  4. Server> Executing /usr/jake/example.com/htdocs/home.php 服务器>执行/usr/jake/example.com/htdocs/home.php
  5. Server> OK, it prints out a "Hello World!", send that to the response. 服务器>确定,它将打印出“ Hello World!”,并将其发送给响应。
  6. User> Ok, /home.php shows "Hello World!" 用户>好的,/ /home.php显示“ Hello World!”。

However, most MVC framework (Rails included) goes something like this: 但是,大多数MVC框架(包括Rails)都是这样的:

  1. Server> Server started, initializing routing modules routes.rb 服务器>服务器启动,初始化路由模块routes.rb
  2. User> Please give me /home , thanks! 用户>请给我/home ,谢谢!
  3. Server> OK, /home , per the routing module, is handled with action ShowHomepage() in controller FrontpageCtr Server> OK,每个路由模块的/home通过控制器FrontpageCtr动作ShowHomepage()处理
  4. Server> Execute FrontPageCtr.ShowHomepage() 服务器>执行FrontPageCtr.ShowHomepage()
  5. Ruby> FrontPageCtr.ShowHomepage() prints "Hello World!" Ruby> FrontPageCtr.ShowHomepage()打印“ Hello World!”
  6. Server> OK, sending "Hello World!" 服务器>确定,发送“ Hello World!” down the pipes! 下水道!
  7. User> Ok, /home shows "Hello World!" 用户>好, /home显示“ 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: 在典型的MVC框架中,处理对任何URL的请求都是这样的:

  1. Look in the Routing module (which in the case of rails is defined in routes.rb ) 查看“路由”模块(在rails中,它是在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 然后,Rails创建Controller并调用Action函数(无论可能是什么)
  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 eg send the user to another URL and whatnot. 然后,该操作的结果将.rhtml “ Rendered”,在这种情况下,该结果被认为是将.rhtml文件呈现为实际的HTML ...当然,还有其他类型的结果,例如,将用户发送到另一个URL等。
  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. 简而言之:您必须先断开脚本和URL的概念。 When you're building MVC websites, they are almost always NOT related in a way that most people understand. 当您构建MVC网站时,它们几乎总是以大多数人理解的方式相关。

With that in mind, you should be more comfortable learning Rails and MVC way of life. 考虑到这一点,您应该更轻松地学习Rails和MVC的生活方式。

I'm not a Rails pro so please correct me if I'm mistaken on any part. 我不是Rails专业人士,所以如果我在任何方面有误,请更正我。

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. 我建议购买和使用Rails进行敏捷Web开发 ,这是一本非常不错的书,也是一种学习Ruby和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. 根据您的描述,您对Ruby和Rails的工作原理有根本的缺陷。 I suggest you spend some time with the book then come back and ask about anything that you get stumped on. 我建议您花一些时间来学习这本书,然后再回去问一下您遇到的任何问题。

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. Ruby on Rails入门一开始有点令人生畏,但是一旦开始,它会变得容易得多。 After running Ruby on Rails bootcamps for Startup Accelerators, Harvard Business School, in Times Square, Boston, and Pittsburgh, I started http://www.firehoseonline.com . 在波士顿时代广场,波士顿和匹兹堡的哈佛商学院为启动加速器运行Ruby on Rails训练营之后,我开始了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. 至于您有关直接打开php文件的问题,使用MVC模式有所不同。 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. 您需要先设置一个控制器,视图和路由,然后才能开始将代码放入.rhtml (或现在的.html.erb )文件中。 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 阿罗哈,肯

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): 如果您找到要修改的正确文件,Rails就是“先解析页面” ;-)要修改的文件位于以下文件夹中:

app/views/... 应用程序/视图/ ...

That's the short answer. 这是简短的答案。 For a comprehensive one (for a newbie), I highly recommend: http://guides.rubyonrails.org/getting_started.html 对于全面的(对于新手),我强烈建议: http : //guides.rubyonrails.org/getting_started.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM