如何:使用 Ruby on Rails 构建基本站点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/987887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How: Ruby on Rails to build a basic site
提问by SztupY
I'm a compsci student who wants to get learn a little about web development -- I learn best by doing. I know basic html/css/php/javascript/xml, but since Ruby is one of my favourite scripting languages, I figured I'd learn Ruby on Rails.
我是一名 compsci 学生,想要学习一些关于 Web 开发的知识——我通过实践学习得最好。我知道基本的 html/css/php/javascript/xml,但由于 Ruby 是我最喜欢的脚本语言之一,我想我会学习 Ruby on Rails。
I'd like to build a basic website for a friend's club at school that just provides information about their organization and services they offer, and have an admin panel on it that contains a very basic inventory system (item, number in inventory, cost -- that's it) in order to learn Ruby on Rails. I'll be hosting it on a computer on campus -- so I don't have to worry about hosting.
我想为学校的朋友俱乐部建立一个基本网站,该网站仅提供有关其组织和所提供服务的信息,并在其上有一个管理面板,其中包含一个非常基本的库存系统(项目,库存数量,成本 - - 就是这样)为了学习 Ruby on Rails。我将在校园内的计算机上托管它——所以我不必担心托管。
This might sound a little silly, but as someone who's never built a website themselves, I was wondering how exactly one goes about it with rails -- like, how do I make a basic layout for the main part of the site -- with things like "Home, About Us, Services, Contact, Club Executive" along the top? Do I have to make it in html and put it in the "view" section? The tutorials I've read on rails (Getting Started With Rails) actually make the basic inventory system seem easy, compared to this part, using a lot of the built in functionality of Rails and scaffolding. The Rails documentation is a tad bit confusing.
这听起来可能有点傻,但作为一个从来没有自己建立过网站的人,我想知道人们究竟是如何使用 Rails 来实现它的——比如,我如何为网站的主要部分制作一个基本的布局——用一些东西像顶部的“主页、关于我们、服务、联系方式、俱乐部行政人员”?我必须在 html 中制作它并将其放在“视图”部分吗?我读过的关于 rails 的教程(Rails 入门)实际上使基本库存系统看起来很简单,与这部分相比,使用了大量 Rails 和脚手架的内置功能。Rails 文档有点混乱。
回答by SztupY
The "official" Rails book is quite good if you want to start building Rails applications. link
如果您想开始构建 Rails 应用程序,那么“官方”Rails 书籍非常好。关联
But actually is something like this:
但实际上是这样的:
- Create the rails app using
rails applicationname - Create the controllers. It seesm that for you one controller is actually enough, name it for example main:
ruby script/generate controller main Now you have a controller in
app/controllers, calledmain_controller.rb. Here you can insert the actions you want this controller to respond. If you don't want the controller to do anything just show the view, then leave the method empty.class Main < ActionPack::Controllers def index end def about end def contact end (...) end
Now you got a controller that will respond to index, about and contact.
- Create the views for this controller in
app/views/main/index.erb(and others, like about.erb) - You can simply use HTML if you want
Alternatively you can use a layout, that you have to define in
app/views/layouts/main.rhtmlIn this layout use HTML, but wherewer you want to include the view, write<%= yield %>Example:<HTML> <BODY> <%= yield %> </BODY> </HTML>
You can include this layout in the controller by writing
layout :mainin the class (before the method declarations)- Now if you run
ruby script/serverin the root of the app you can access the pages you've created. They will be static of course, but this might get you going. You have to add models and some logic to your controllers to advance. I advice you to check the book I linked if you're interested in more, or check the alternatives of rails likemerb(http://merbivore.org) which has some nice features and is usually faster, but lacks the maturity of rails.
- 使用创建 rails 应用程序
rails applicationname - 创建控制器。看起来对你来说一个控制器实际上就足够了,例如将其命名为 main:
ruby script/generate controller main 现在您在 中有一个控制器
app/controllers,称为main_controller.rb。您可以在此处插入您希望此控制器响应的操作。如果您不希望控制器执行任何操作,只显示视图,则将该方法留空。class Main < ActionPack::Controllers def index end def about end def contact end (...) end
现在你有了一个可以响应索引、关于和联系的控制器。
- 在
app/views/main/index.erb(和其他人,如about.erb)中为此控制器创建视图 - 如果需要,您可以简单地使用 HTML
或者,您可以使用必须
app/views/layouts/main.rhtml在此布局中定义的布局,使用 HTML,但要在何处包含视图,请编写<%= yield %>示例:<HTML> <BODY> <%= yield %> </BODY> </HTML>
您可以通过
layout :main在类中编写(在方法声明之前)来将此布局包含在控制器中- 现在,如果您
ruby script/server在应用程序的根目录中运行,则可以访问您创建的页面。它们当然会是静态的,但这可能会让你继续前进。您必须向控制器添加模型和一些逻辑才能前进。如果您对更多内容感兴趣,我建议您查看我链接的书,或者查看 Rails 的替代品,例如merb( http://merbivore.org),它具有一些不错的功能并且通常速度更快,但缺乏 Rails 的成熟度。
回答by Robyn Smith
I picked up the book "Agile Web Development with Rails", and it's excellent. It goes through building an online grocery cart.
我拿起了“使用 Rails 进行敏捷 Web 开发”这本书,它非常棒。它通过建立一个在线杂货车。
回答by Alan Haggai Alavi
You should read about Model-View-Controllerarchitecture if you have not yet done so, as it is the basis for most web frameworks including Ruby on Rails.
如果您还没有阅读过模型-视图-控制器架构,那么您应该阅读它,因为它是包括 Ruby on Rails 在内的大多数 Web 框架的基础。
回答by btw
It sounds to me like this site might not be the best way to learn Ruby on Rails. Rails is really great for CRUD applications (applications which allow users to Create, Read, Update, and Delete records in a database). Since your site looks to be all static pages except for the "Contact Us" section (which I'm assuming is a form that sends an email with some kind of confirmation page), you're actually going to find yourself kind of fighting against "The Rails Way."
在我看来,这个站点可能不是学习 Ruby on Rails 的最佳方式。Rails 非常适合 CRUD 应用程序(允许用户在数据库中创建、读取、更新和删除记录的应用程序)。由于您的网站看起来都是静态页面,除了“联系我们”部分(我假设它是一种发送带有某种确认页面的电子邮件的表单),您实际上会发现自己有点反对“铁轨之路。”
Ideally in a situation like this, you could just throw all of your static pages into the public/ directory and make a quick Rails scaffold for the "Contact Us" page.
理想情况下,在这种情况下,您可以将所有静态页面放入 public/ 目录,并为“联系我们”页面创建一个快速的 Rails 脚手架。
But by doing that, you won't end up with a finished project that resembles a typical Ruby on Rails application, and in the worst case, you might find yourself having to "unlearn" or at least "relearn" a lot of the aspects of Rails programming.
但是通过这样做,您最终不会得到一个类似于典型 Ruby on Rails 应用程序的完成项目,在最坏的情况下,您可能会发现自己不得不“忘记”或至少“重新学习”很多方面Rails 编程。
I think building a CRUD application with several resources (the canonical "Rails blog in 15 minutes"is a great start. You'll learn more by practicing Rails conventions and seeing the kind of workflow and application that really allows Rails to shine.
我认为使用多种资源构建 CRUD 应用程序(规范的“15 分钟内的 Rails 博客”是一个很好的开始。您将通过练习 Rails 约定和了解真正让 Rails 大放异彩的工作流和应用程序类型来了解更多信息。
Then when it comes time to build another mostly-static website, you'll know exactly what you'll need to do to go about it.
然后,当需要构建另一个主要是静态的网站时,您将确切地知道需要做什么来实现它。
My 2 cents, anyway.
无论如何,我的2美分。
回答by Ryan Oberoi
Start off with Mephisto. It will give a framework to achieve your goal rather fast... otherwise you may simply flounder learning the gazillion things involved in creating the rails website.
从墨菲斯托开始。它将提供一个框架来相当快地实现您的目标……否则您可能只是在学习创建 rails 网站所涉及的无数事物时陷入困境。
回答by Dave Everitt
With a simple site, I'd go for a Ruby micro-framework. The three I like are: Sinatra, Ramazeor _why's 4k Camping(get the one with the bugs fixed). RoR would be overkill.
对于一个简单的站点,我会选择Ruby 微框架。我喜欢的三个是:Sinatra、Ramaze或 _why 的 4k Camping(找一个修复了错误的)。RoR 会矫枉过正。
回答by Pistos
I must recommend Ramaze. If you already know Ruby, but don't know Rails yet, Ramaze is better suited to you because it is "closer to home" as far as Ramaze apps being pure(r) Ruby.
我必须推荐拉马泽。如果您已经了解 Ruby,但还不了解 Rails,那么 Ramaze 更适合您,因为就 Ramaze 应用程序是纯 (r) Ruby 而言,它“离家更近”。
For your DB access, you get a choice of ORMs. Sequel is most popular among Ramazers, but there's also DataMapper and M4DBI.
对于您的数据库访问,您可以选择 ORM。Sequel 在 Ramazers 中最受欢迎,但也有 DataMapper 和 M4DBI。
As Alan Alavi already said: You should familiarize yourself with MVC, but that can be done simply by diving in and getting your hands dirty.
正如 Alan Alavi 已经说过的那样:您应该熟悉 MVC,但这可以通过深入了解并亲自动手来完成。

