![Page 1: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/1.jpg)
Ruby on Rails
Your first app
![Page 2: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/2.jpg)
Rails files
app/Contains the controllers, models, views and assets for your application. You’ll focus on this folder for the remainder of this guide.
config/Configure your application’s runtime rules, routes, database, and more. This is covered in more detail in Configuring Rails Applications
db/Contains your current database schema, as well as the database migrations.
doc/ In-depth documentation for your application.
lib/ Extended modules for your application.
![Page 3: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/3.jpg)
Rails fileslog/ Application log files.
public/The only folder seen to the world as-is. Contains the static files and compiled assets.
script/Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.
test/Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications
config.ru Rack configuration for Rack based servers used to start the application.
GemfileGemfile.lock
These files allow you to specify what gem dependencies are needed for your Rails application.
![Page 4: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/4.jpg)
Configuring your DB
• SQLite: built-in support, lightweight, serverless
development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000
![Page 5: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/5.jpg)
Configuring your DB
• MySQL: not built in, you need to configure properly the onfig/database.yml
development: adapter: postgresql encoding: unicode database: blog_development pool: 5 username: blog password
![Page 6: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/6.jpg)
Create an empty DB$ rake db:create
![Page 7: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/7.jpg)
Hello Rails!
• You need to create at minimum a controller and a view.
• You can do that in a single command. Enter this command in your terminal:
rails generate controller home index
![Page 8: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/8.jpg)
erb
• Rails will create several files for you• app/views/home/index.html.erb: is the
template that will be used to display the results of the index action (method) in the home controller.
• Edit this file in your text editor and edit it to contain a single line of code:
<h1>Hello, Rails!</h1>
![Page 9: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/9.jpg)
Setting your application homepage
• Open: config/routes.rb
$ rm public/index.html
# You can have the root of your site routed with "root" # just remember to delete public/index.html. # root :to => 'welcome#index' root :to => "home#index"
![Page 10: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/10.jpg)
Scaffolding
• Rails scaffolding is a quick way to generate some of the major pieces of an application.
• If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
![Page 11: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/11.jpg)
Blog Example
• Let’s create a blog app with Rails• Using Scaffold to create a Post resource that
represents a single blog post:rails generate scaffold Post name:string title:string content:text
![Page 12: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/12.jpg)
What Scaffold generator created
db/migrate/20100207214725_create_posts.rb
Migration to create the posts table in your database (your name will include a different timestamp)
app/models/post.rb The Post model
test/unit/post_test.rb Unit testing harness for the posts model
test/fixtures/posts.yml Sample posts for use in testing
config/routes.rb Edited to include routing information for posts
app/controllers/posts_controller.rb The Posts controller
app/views/posts/index.html.erb A view to display an index of all posts
app/views/posts/edit.html.erb A view to edit an existing post
app/views/posts/show.html.erb A view to display a single post
app/views/posts/new.html.erb A view to create a new post
![Page 13: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/13.jpg)
What Scaffold generator created
app/views/posts/_form.html.erbA partial to control the overall look and feel of the form used in edit and new views
test/functional/posts_controller_test.rb Functional testing harness for the posts controller
app/helpers/posts_helper.rb Helper functions to be used from the post views
test/unit/helpers/posts_helper_test.rb Unit testing harness for the posts helper
app/assets/javascripts/posts.js.coffee CoffeeScript for the posts controller
app/assets/stylesheets/posts.css.scss Cascading style sheet for the posts controller
app/assets/stylesheets/scaffolds.css.scss Cascading style sheet to make the scaffolded views look better
![Page 14: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/14.jpg)
Database Migration
• Migrations are Ruby classes that are designed to make it simple to create and modify database tables.
• Use “rake”commands• Look in
the db/migrate/20100207214725_create_posts.rb (yours will have a slightly different name)– When you run this migration it will create a posts table
with two string columns and a text column. – It also creates two timestamp fields to allow Rails to track
post creation and update times.
![Page 15: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/15.jpg)
Try a migration
• More info at: http://guides.rubyonrails.org/migrations.html
rake db:migrate
![Page 16: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/16.jpg)
Add a link
• app/views/home/index.html.erb
<h1>Hello, Rails!</h1><%= link_to "My Blog", posts_path %>
![Page 17: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/17.jpg)
The Model
• Check app/models/post.rb • The Post class inherits from ActiveRecord::Base. • Active Record supplies a great deal of functionality to
your Rails models for free, including – basic database CRUD (Create, Read, Update, Destroy)
operations, – data validation, – sophisticated search support – ability to relate multiple models to one another.
![Page 18: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/18.jpg)
The Model
• attr_accessible: Remember Ruby OO?• It specifies a whitelist of attributes that are allowed
to be updated in bulk (via update_attributes for instance).
![Page 19: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/19.jpg)
Add Validation
• Edit the model:
• Validation overview: http://guides.rubyonrails.org/active_record_validations_callbacks.html#validations-overview
class Post < ActiveRecord::Base attr_accessible :content, :name, :title
validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } end
![Page 20: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/20.jpg)
Test validation$ rails console
>> p = Post.new(:content => "A new post")=> #<Post id: nil, name: nil, title: nil, content: "A new post", created_at: nil, updated_at: nil>>> p.errors.full_messages=> ["Name can't be blank", "Title can't be blank", "Title is too short (minimum is
![Page 21: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/21.jpg)
Listing all posts
• Open the file app/controllers/posts_controller.rb
• Layouts and rendering: http://guides.rubyonrails.org/layouts_and_rendering.html
def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render :json => @posts } endend
![Page 22: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/22.jpg)
Layout
• Application specific layout in: app/views/layouts/application.html.erb
• Edit! Make the body background a different color (other than white!)
![Page 23: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/23.jpg)
Creating new posts
• Instantiate new Post: where??• Display a new Post: where??• Open: views/posts/_form.html.erb – Where are all these classes defined?– What is the: @post?– What is the form_for block:
• used to create an HTML form. Within this block, you have access to methods to build various controls on the form
• smart enough to work out if you are doing a New Post or an Edit Postaction, and will set the form action tags and submit button names appropriately in the HTMLoutput.
![Page 24: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/24.jpg)
Creating new posts
• What happens when you hit the create button?– POST or GET action?
• If not successfully created: return to new!• If successfully created store to Rails flash hash
![Page 25: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/25.jpg)
Display an individual post
• http://localhost:3000/posts/1• Where is the code that shows post?– There are to parts: show action– Erb part
![Page 26: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/26.jpg)
Edit posts
• Two parts again! Find them.• Actions:– Edit– Update
![Page 27: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/27.jpg)
Delete posts
• What type of action?• How is the new page displayed?
![Page 28: Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for](https://reader030.vdocuments.net/reader030/viewer/2022032612/56649eae5503460f94bb538e/html5/thumbnails/28.jpg)
Resources
• http://guides.rubyonrails.org/getting_started.html
• http://ruby.railstutorial.org/chapters/a-demo-app#top
• http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol