Escolar Documentos
Profissional Documentos
Cultura Documentos
From DevchixWiki
Contents
1 Build and deploy a web application
1.1 Let's build a web application
2 Make it your own
2.1 Edit index page
2.2 Awesome! Let's ship it!
3 A Closer Look at the Features
4 First Feature: Adding topics
4.1 Scaffolding
4.2 A Quick Look at Your Database
4.3 Deploy Your Application
5 What did we just do?
5.1 A closer look
6 Routes
7 Next feature: creating a topic
7.1 Controller: adjusting the flow of your application
8 Next feature: the Topics page
9 Next feature: allow voting on a topic
9.1 How will we build this feature?
9.2 Rails associations
9.3 Add votes
9.4 Allow people to vote
Everything you need for your application is in this new folder. If you decide you don't like it, just delete it
and start over with the rails command.
Note that the -m option is just a shortcut for a few commands listed in the github file (click here to see
(http://gist.github.com/194076.txt) ). It is optional. Usually you will not use that option, or maybe you will
create your own template with your frequently used options). We'll explain them in more detail later.
Go to your new folder. Notice that Rails has created the following subdirectories within it:
File/Folder Purpose
README This is a brief instruction manual for your application.
Rakefile This file contains batch jobs that can be run from the terminal.
Contains the controllers, models, and views for your application. You will do most of your
app/
work here.
config/ Configure your application’s runtime rules, routes, database, and more.
db/ Shows your current database schema, as well as the database migrations.
doc/ You would add documentation for your application here
features/ Added by the template. This is from cucumber, which is not part of core Rails
lib/ Extended modules for your application (not covered today)
log/ Application log files
The only folder seen to the world as-is. This is where your images, JavaScript, stylesheets
public/
(CSS), and other static files go
script/ Scripts provided by Rails to do recurring tasks. We'll use some today
test/ Unit tests, fixtures, and other test apparatus
tmp/ Temporary files
vendor/ A place for third-party code
ruby script/server
Point your web browser to http://localhost:3000, and see your web app actually there!
git init
git add .
git commit -m 'basic web application'
heroku create
git push heroku master
Notice the URL that heroku reports after you typed "heroku create." Type this URL in your browser to see
your application on the Web.
One of the basic features that we might build first is defined in the topics.feature file in the features
directory:
Feature: Topics
In order to see a list of potential topics for meetings
people need to be able to create and edit them
cucumber features/topics.feature
Of course, the feature fails because we haven't written any code yet! These feature descriptions are tests as
well as documentation. Typically, we run the test, watch it fail, then implement a feature, then run the test
again to see if it passes. We'll be doing that today.
The "topics" feature relies on some basic elements of a web app, which is what we'll build first. As we get
features to pass, we'll look further at the features definitions to see what needs to be built.
Scaffolding
Run this command line script to generate a Topic model along with the application logic and views:
db/migrate/20091014021209_create_topics.rb
See how it contains Ruby code to set up the database table. Migrations can also be used for modifying
tables (add/remove/rename columns) and even modifying data.
Now let's set up the database for the test by running the migration file on the test database:
Run the cucumber feature again (and the first one should pass)
cucumber features/topics.feature
Now let's look at the feature we created. We will typically do so in the development environment.
Create the topics table:
rake db:migrate
ruby script/server
Congratulations! You have built a web application that works with a relational database.
Click the open folder icon ! or choose Database -> Connect Database to open suggestotron/db/
development.sqlite3
In SQLite, you can choose “Browse & Search” to interactively explore the database or “Execute SQL” to
type SQL commands.
You can also access the database through the command line:
ruby script/dbconsole
Common SQL Commands
sqlite MySql
list tables in current db .tables show tables;
.schema show create table topics;
show SQL for table create list columns
.schema people describe topics;
git add .
git commit -m 'topic crud'
git push heroku master
heroku rake db:migrate
Congratulations! You have built and deployed a web application that works with a relational database.
Model
View
Controller
When you executed the script/generate command, Rails generated files that implement a model, views, and
a controller for the topics feature.
The Model
create app/models/topic.rb
create db/migrate/20090611073227_create_topics.rb
create app/views/topics/index.html.erb
create app/views/topics/show.html.erb
create app/views/topics/new.html.erb
create app/views/topics/edit.html.erb
The Controller
create app/controllers/topics_controller.rb
route map.resources :topics
A closer look
Rails allows you to easily invoke irb with all of the Rails libraries and your application code loaded:
ruby script/console
>> t = Topic.new
=> #<Topic id: nil, title: nil, description: nil, created_at: nil, updated_at: nil>
>> t.title = "My topic"
=> "My topic"
>> t.description = "this is really cool"
=> "this is really cool"
>> t.save
Notice that the Topic class has title and description attributes which you did not need to explicitly declare
in the class. This is handled by ActiveRecord which implements ORM (Object Relational Mapping) in
Rails.
Routes
Rails routes control how URLs map to code. We can use rake to list the relationships between routes and
controllers.
$ rake routes
topics GET /topics(.:format) {:action=>"index", :controller=>"topics"}
POST /topics(.:format) {:action=>"create", :controller=>"topics"}
new_topic GET /topics/new(.:format) {:action=>"new", :controller=>"topics"}
edit_topic GET /topics/:id/edit(.:format) {:action=>"edit", :controller=>"topics"}
topic GET /topics/:id(.:format) {:action=>"show", :controller=>"topics"}
PUT /topics/:id(.:format) {:action=>"update", :controller=>"topics"}
DELETE /topics/:id(.:format) {:action=>"destroy", :controller=>"topics"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
Each method in the controller will take an HTTP request, usually find some data in the database (via an
ActiveRecord model) and render a view or re-direct to another action.
cucumber features/topics.feature
Run the server ( script/server), look at the app (http://localhost:3000/topics/), and see how the scaffold
template differs from the desired application as we've described it above using cucumber. What we want is
the application to show the list of topics after the user creates a topic by completing the form. However,
the scaffold displays instead a page showing only the individual topic the user just created.
format.html { redirect_to(topics_path) }
Now run your feature again with cucumber and it should pass
cucumber features/topics.feature
cucumber features/topics_list_and_details.feature
In the last scenario we made it so the app redirected to "topics_path" after create. So, we expect that
we will need to fix the error in that page. We can use rake routes to find the controller action, then
we can see that the default view is rendered. The error is in views/topics/index.html.erb:
Run the scenario again and see that the "Clicking on the topic title" passes, but we still have one
failure which can be addressed in the same file
Open views/topics/index.html.erb and change "Destroy" to "Delete".
cucumber features/votes.feature
Rails associations
Add votes
Use cucumber to run the "votes" feature
cucumber features/votes.feature
We will use the resource generation script to create a model and controller (no views):
>> t = Topic.new
=> #<Topic id: nil, title: nil, description: nil, created_at: nil, updated_at: nil>
>> t.votes
=> []
>> t.votes.build
=> #<Vote id: nil, topic_id: nil, created_at: nil, updated_at: nil>
>> t.votes
=> [#<Vote id: nil, topic_id: nil, created_at: nil, updated_at: nil>]
>> t.save
=> true
>> t.votes
=> [#<Vote id: 2, topic_id: 2, created_at: "2010-02-22 01:42:27", updated_at: "2010-02-22 01:42:27">]
Check rake routes for figuring out the pathTODO: diagram or screenshot of where this info is on the rake output , and
edit your view ( /app/views/topics/index.html.erb):
<td><%= link_to '+1', votes_path(:topic_id => topic.id), :method => :post %></td>
Now we need to create the controller action. Open the Votes controller
( /app/controllers/votes_controller.rb) to add the create action: