- 浏览: 3502815 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
Want to get a taste of what it's like to develop with Rails? Follow this tutorial to get started!
Part 1 - Installation
Install Ruby/Rails for your particular platform, and make sure you have SQLite installed (this comes with a Mac if you have a Mac). Installation wiki pages can be found here: Mac , Windows , Linux .
Part 2 - Create & Run
-
Run the Rails command line tool to create the application.
$ rails myapp
This will create the skeleton of your Rails app.
-
Go into your directory, probably by typing:
$ cd myapp
-
Call up a browser and go to http://localhost:3000 . You should see that Rails is running; that's all there is to it! Pat yourself on the back, you deserve it.
Part 3 - Creating the Database & Scaffold
-
Open the application in your editor of choice.
-
Open the
config/database.yml
file. This is where you would typically configure different databases for your application, but for the sake of this example you shouldn't have to touch anything. The default database names, as you may have noticed, are taken from the name of your Rails app. -
Open up another command prompt (that way we can keep the server running in the background), and in your application directory run:
$ rake db:create
This will create your development database.
-
Now we want to create our first table and interface. In Rails, this typically means we're going to create a scaffold to start from. A scaffold is simply a starting point, and we can add increasingly complex code from there. Let's first run the scaffold command without any parameters:
$ ruby script/generate scaffold
Take a look at the output generated. It shows you how to use the command.
-
Now let's go ahead and use scaffold to start generating our web application. We're going to create a users table:
$ ruby script/generate scaffold user first_name:string last_name:string active:boolean
It's okay if you copy and paste this code, I won't tell anyone. So now your scaffold is created, which is just a bunch of template files.
-
Let's run the migration it created for us (to create the database table).
$ rake db:migrate
-
Now go back to your browser (start your server back up if you killed it), and go to http://localhost:3000/users
-
Create a few users, and play around with your Rails interface!
Part 4 - Adding Functionality
-
We just realized that we forgot to add a bio field for a user - oops! We could either create a new migration to add that column, or roll back the migration that was created for us. Let's do the latter. To remove the table so we can add a column, run this command:
$ rake db:rollback
This will roll back one migration.
-
Now open the migration located in the
/db/migrations/
or/db/migrate/
directory. It should be the only file. -
Notice how the migration has an
up
method to migrate up, anddown
method to migrate down. -
We want to add that bio field, so after the
t.boolean :active
line, add:t.text :bio
-
Save the file, and run
rake db:migrate
again to migrate your database up:$ rake db:migrate
-
If you checked your browser at this point, you'd notice that the bio field isn't automatically showing up in our user's information or forms. We will need to add this ourselves. Yes, we could have erased the files and run the scaffold command to get this field, but then you wouldn't learn anything.
-
Lets open up the
/app/views/users/new.html.erb
file. This is what renders our New User form. We need to add a bio field using the following code below the active checkbox.< p> <% = f.label :bio %>< br /> <% = f.text_area :bio %> </ p>
-
Now open up the New User page in your browser http://localhost:3000/users/new . You should see the bio field, and be able to add new users with a bio. For extra credit, you could use the same code if you wanted to update the
edit
view (in/app/views/users/edit.html.erb
) so you can edit a user's bio. -
When you create a user, you're brought automatically to his/her show page. Let's add the bio to the show page. You can do this by opening the
/app/views/users/show.html.erb
file, and underneath the Active field, adding:< p> < b> Bio:</ b> <% =h @user .bio %> </ p>
-
Save the file and check it out in the browser. If you don't see your change at first, make sure you are on the
show
page (and not theindex
). When you get to this part, let out a barbaric “woot!”, you've made some great progress.
Part 5 - Your First Validation
-
In Rails, we put our validations in our model files, so let's open
/app/models/user.rb
. -
We want to require that a user enter their first name and last name, so let's add validation to our User model, so it ends up looking like this:
class User < ActiveRecord::Base validates_presence_of :first_name , :last_name end
-
Save the file, go to your browser, and attempt to create a user without a first name or last name.
-
Rails has many helper validation classes, and you can see a list on the right side of this API page .
For extra credit, add another validation to your app just for fun.
Part 6 - Modifying the Layout
-
If you viewed the browser HTML source of your application, you may have noticed that your application is in some sort of layout. By default, when you ran the
scaffold
command, a/app/views/layouts/users.html.erb
file was created, and used as your layout. -
You'll probably only want to use one layout for most of your controllers, so let's give this a more generic name. Rename
users.html.erb
toapplication.html.erb
. As a Rails convention, now that you have anapplication.html.erb
file in your layouts directory, all controllers will use this by default (you can override this later if you want). -
Let's add something to our layout just for fun. Open up the
application.html.erb
and add something like:<h2>Rails Rocks!</h2>
-
Save the file, go to your browser, and you should see the change you made reflected on ALL pages of the website.
Part 7 - Having fun with the console
-
Let's have some fun playing with our User Model. In your command prompt, type:
$ ruby script/console
You are now in a Ruby IRB session which has access to your models.
-
Before we start playing, run the following command so that we can see what SQL commands are going to be generated for us.
$ ActiveRecord::Base.logger = Logger.new(STDOUT)
-
Let's try a simple Ruby statement to make sure everything is working:
puts "Hello Dude!"
-
Now let's try having some fun with our model. Create a new model by doing something like this:
u = User.new u.first_name = "Gregg" u.last_name = "Pollack" u.save
Notice the SQL that is generated.
-
Now try doing it all in a single line:
User.create ( :first_name => "Joe" , :last_name => "Blow" )
If you hit any errors, check for syntax problems.
-
Let's fetch the user with first name of Joe, and change his last name to “Johnson”.
u = User.find_by_first_name ( "Joe" ) u.last_name = "Johnson" u.save
Notice the SQL that gets generated; nice!
-
Now let's delete the user.
u.destroy
-
Have some fun trying the following commands to see what they do:
User.first User.last User.all
Feel free to set a variable with the values, and play around with changing attributes. Do note that
.all
returns an array, so you may want to use [0] or [2] to refer to a particular item in the array. -
Let's print out all the users in your system in upper case:
User.all .each { | user| puts user.first_name .upcase }
-
You'll notice that it prints out the first names, and then it also returns the array of Users. Remember, every method in Ruby returns something, even if it's nil.
-
Let's create an array of all of the last names in our system:
User.all .map { | user| user.last_name }
What we're interested in here is the return value, which should be an array of last names.
-
Take a look at the API page here . Along the right you'll see all of the methods you can run on ActiveRecord objects. Try a few.
Additional Reading
The Getting Started with Rails Guide contains another fun walkthrough, but with additional explanation of Rails architecture.
发表评论
-
undefined method `length' for Enumerable when calling truncate method
2010-03-17 00:58 1962With rails 2.0.2 and ruby 1.8.7 ... -
Installing ruby-oci8 on Ubuntu
2010-02-18 21:42 4270osted by 2muchtea under LD_LIBR ... -
localization插件实现Rails多语言支持
2010-02-18 20:27 3508local ... -
“Ruby on Rails 之 Oracle 应用”常见问题解答
2010-02-16 22:33 2328目录 为什么使用 RUBY ON RAILS? ... -
How to create a Ruby extension in C in under 5 minutes
2010-02-15 17:09 2693By Peter Cooper — June 18th, ... -
Ruby也能写servlet
2010-02-15 15:17 1873出处/blogjava Ruby也能写servl ... -
在Linux平台上安装和配置Ruby on Rails详解
2010-02-12 11:46 1791uby on rails推荐的生产运行环境是Linux/Fre ... -
ruby on rails
2010-02-11 13:55 1791ruby on rails rubygems R ... -
转:Ruby on rails 2.0.2傻瓜入门之Hello world
2010-02-10 15:00 2162毫不例外,和所有其他的开源项目一样,如果你想进入Ruby on ... -
Ruby on Rails 通过代理远程安装
2010-02-10 14:20 2377在网上查了一些资料,都不详细,现在列出标准命令: 1。如果代 ... -
JRuby wiki
2010-02-02 00:28 1732http://kenai.com/projects/jruby ...
相关推荐
《Head First Rails》是为学习Ruby on Rails的学习者提供的一个伴侣手册。本书是Head First系列的一部分,该系列书籍以其结合实际应用场景的教育方式而著称,旨在帮助读者快速掌握技能并迅速上手。《Head First ...
英文版,文字版, 带目录。
《Head First Rails中文版.2011.12》这本书是针对初学者设计的一本深入浅出的Rails教程,旨在帮助读者快速掌握Ruby on Rails框架。Ruby on Rails(简称Rails)是由David Heinemeier Hansson开发的一个开源Web应用...
《Head First Rails》是一本专注于Ruby on Rails框架的编程书籍,旨在帮助读者快速掌握Rails的核心概念和开发技能,以便能够高效地构建下一代互动式Web应用。Rails是一种流行的Web开发框架,它遵循MVC(模型-视图-...
Ruby on Rails教程:первоеприложение ...*应用程序根目录/ home / msks / rails_projects / first_app *环境发展 *数据库适配器sqlite3 如果您不打算运行rake doc:app,请随意使用其他标记语言。
在Ruby on Rails(RoR)框架中,"RailsTutorial_FirstApp"是一个常见的起点,用于学习和实践RoR的基础知识。这个教程旨在引导开发者创建他们的第一个Web应用程序,这通常包括了解RoR的核心概念、MVC(模型-视图-控制...
你将学习一切Rails scaffolding的基本原理,以创建自定义的交互式网络应用程序,全部使用Rails的一套丰富的工具和MVC框架。 你将掌握数据库交互、Ajax和XML的集成、丰富的内容,甚至数据的动态图形——曾经要使用...
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
创建新的Rails应用程序非常简单,只需在终端中输入`rails new first_app`。这将会生成一系列的目录和文件,它们组成了Rails应用的基本结构。`first_app`是你指定的应用名称,这里替换为`first_app`。 接下来,我们...
在Ruby on Rails教程中,"first_app"是一个基础项目,旨在引导初学者了解Rails框架的基本概念和工作流程。Ruby on Rails(简称Rails)是用Ruby语言编写的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构...
在Ruby on Rails(RoR)教程中,"First_app"是一个经典的起点,它引导初学者构建他们的第一个Web应用。这个教程旨在教你如何利用RoR强大的框架特性,快速、高效地开发动态网页。让我们深入探讨一下这个过程中涉及的...
这本书通过实际的示例项目“sample_app”引导读者深入理解Rails框架的各个方面。现在,我们来详细探讨这个源代码包中的知识点。 1. **Ruby on Rails框架基础**:Rails是一个基于Ruby语言的开源Web应用框架,遵循MVC...
RailsApp是基于Ruby on Rails框架构建的一个应用实例。Ruby on Rails(简称Rails)是一个开源的Web应用程序框架,它遵循模型-视图-控制器(MVC)架构模式,用于快速开发高效、可维护的Web应用。Rails的核心理念是...
Rails项目创建后,Aptana会自动生成基础的目录结构,包括app、config、db、lib等关键文件夹。在app目录下,你可以找到views、controllers、models等子目录,它们分别对应视图、控制器和模型三个核心组件。 接下来,...
For those new to Rails, this book provides a quick introduction, the big picture, a walk through the installation process, and some tips on getting started. If you've already started working with ...
git clone https://github.com/MTayyebKhan/railsapp 导航到目录 cd manga_application 安装所需的宝石 bundle install 创建数据库,运行迁移并使用示例数据播种数据库。 rails db:setup 可选:如果您不需要种子...
This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...