SlideShare a Scribd company logo
1 of 22
Download to read offline
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                             http://guides.rubyonrails.org/command_line.html


               More at rubyonrails.org: Overview | Download | Deploy | Code | Screencasts | Documentation | Ecosystem | Community | Blog




               A Guide to The Rails Command Line
               Rails comes with every command line tool you’ll need to

                    Create a Rails application

                    Generate models, controllers, database migrations, and unit tests

                    Start a development server

                    Mess with objects through an interactive shell

                    Profile and benchmark your new creation


               This tutorial assumes you have basic Rails knowledge from reading the
               Getting Started with Rails Guide.

                           Chapters
                        1. Command Line Basics




1 sur 22                                                                                                                                                         20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                    http://guides.rubyonrails.org/command_line.html




               2. The Rails Advanced Command Line
                     Rails with Databases and SCM

                            with Different Backends

                     The Rails Generation: Generators

                     Rake is Ruby Make



                     This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.




             1 Command Line Basics
             There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you’ll probably use them are:




             Let’s create a simple Rails application to step through each of these commands in context.


             1.1
             The first thing we’ll want to do is create a new Rails application by running the            command after installing Rails.



                     You know you need the rails gem installed by typing                         first, if you don’t have this installed, follow the instructions in the Rails 3



2 sur 22                                                                                                                                                                                20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                              http://guides.rubyonrails.org/command_line.html


                      Release Notes




             Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You’ve got the entire Rails directory structure now with all the code you
             need to run our simple application right out of the box.


             This output will seem very familiar when we get to the           command. Creepy foreshadowing!


             1.2
             Let’s try it! The               command launches a small web server named WEBrick which comes bundled with Ruby. You’ll use this any time you want to view
             your work through a web browser.




3 sur 22                                                                                                                                                                            20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                            http://guides.rubyonrails.org/command_line.html


             WEBrick isn’t your only option for serving Rails. We’ll get to that in a later section.


             Without any prodding of any kind,                    will run our new shiny Rails app:




             With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open http://localhost:3000, you will see a basic rails app
             running.


             1.3
             The                     command uses templates to create a whole lot of things. You can always find out what’s available by running                   by itself. Let’s
             do that:




4 sur 22                                                                                                                                                                         20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                             http://guides.rubyonrails.org/command_line.html




             You can install more generators through generator gems, portions of plugins you’ll undoubtedly install, and you can even create your own!


             Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend
             time writing. That’s what we have computers for.


             Let’s make our own controller with the controller generator. But what command should we use? Let’s ask the generator:


             All Rails console utilities have help text. As with most *NIX utilities, you can try adding   or   to the end, for example                          .




5 sur 22                                                                                                                                                                         20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                   http://guides.rubyonrails.org/command_line.html




             The controller generator is expecting parameters in the form of                                                                     . Let’s make a              controller
             with an action of hello, which will say something nice to us.




             What all did this generate? It made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a
             view file.


             Check out the controller and modify it a little (in                                                  ):ma




6 sur 22                                                                                                                                                                                  20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                     http://guides.rubyonrails.org/command_line.html




             Then the view, to display our message (in                               ):




             Deal. Go check it out in your browser. Fire up your server. Remember?        at the root of your Rails application should do it.




                     Make sure that you do not have any “tilde backup” files in                 , or else WEBrick will not show the expected output. This seems to
                     be a bug in Rails 2.3.0.




             The URL will be http://localhost:3000/greetings/hello.



7 sur 22                                                                                                                                                                 20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                       http://guides.rubyonrails.org/command_line.html


             With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit
             the index action of that controller.


             Rails comes with a generator for data models too:




             For a list of available field types, refer to the API documentation for the column method for the                           class.


             But instead of generating a model directly (which we’ll be doing later), let’s set up a scaffold. A scaffold in Rails is a full set of model, database migration for that
             model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.


             We will set up a simple resource called “HighScore” that will keep track of our highest score on video games we play.




8 sur 22                                                                                                                                                                                    20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line   http://guides.rubyonrails.org/command_line.html




9 sur 22                                                                               20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                   http://guides.rubyonrails.org/command_line.html




             The generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller,
             model and database migration for HighScore (creating the                    table and fields), takes care of the route for the resource, and new tests for everything.


             The migration requires that we migrate, that is, run some Ruby code (living in that                                                  ) to modify the schema of our
             database. Which database? The sqlite3 database that Rails will create for you when we run the                           command. We’ll talk more about Rake in-depth in
             a little while.




             Let’s talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and
             test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your
             code, the better. Seriously. We’ll make one in a moment.


             Let’s see the interface Rails created for us.




             Go to your browser and open http://localhost:3000/high_scores, now we can create new high scores (55,160 on Space Invaders!)


             1.4
             The               command lets you interact with your Rails application from the command line. On the underside,                    uses IRB, so if you’ve ever used it,



10 sur 22                                                                                                                                                                               20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                    http://guides.rubyonrails.org/command_line.html


             you’ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.


             1.5
                                  figures out which database you’re using and drops you into whichever command line interface you would use with it (and figures out the command
             line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.


             1.6
             The                  command simplifies plugin management; think a miniature version of the Gem utility. Let’s walk through installing a plugin. You can call the
             sub-command               , which sifts through repositories looking for plugins, or call         to add a specific repository of plugins, or you can specify the plugin
             location directly.


             Let’s say you’re creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be
             deleted. Wouldn’t it be great if we could override the behavior of a model to never actually take its record out of the database, but instead, just set a field?


             There is such a thing! The plugin we’re installing is called                       , and it lets models implement a              column that gets set when you call
             destroy. Later, when calling find, the plugin will tack on a database check to filter out “deleted” things.




             1.7
                      runs Ruby code in the context of Rails non-interactively. For instance:




11 sur 22                                                                                                                                                                               20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                              http://guides.rubyonrails.org/command_line.html



             1.8
             Think of          as the opposite of         . It’ll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many
             times!




12 sur 22                                                                                                                                                                           20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                http://guides.rubyonrails.org/command_line.html




             1.9
             Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application’s folder, the current Rails environment name, your app’s database
             adapter, and schema version!            is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing
             Rails installation.




             2 The Rails Advanced Command Line
             The more advanced uses of the command line are focused around finding useful (even surprising at times) options in the utilities, and fitting utilities to your needs and
             specific work flow. Listed here are some tricks up Rails’ sleeve.


             2.1 Rails with Databases and SCM
             When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is
             going to use. This will save you a few minutes, and certainly many keystrokes.


             Let’s see what a         option and a                               option will do for us:




13 sur 22                                                                                                                                                                           20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                  http://guides.rubyonrails.org/command_line.html




             We had to create the gitapp directory and initialize an empty git repository before Rails would add files it created to our repository. Let’s see what it put in our
             database configuration:




14 sur 22                                                                                                                                                                             20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                          http://guides.rubyonrails.org/command_line.html




             It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options
             is that you have to make your application’s directory first, then initialize your SCM, then you can run the       command to generate the basis of your app.


             2.2             with Different Backends
             Many people have created a large number different web servers in Ruby, and many of them can be used to run Rails. Since version 2.3, Rails uses Rack to serve its
             webpages, which means that any webserver that implements a Rack handler can be used. This includes WEBrick, Mongrel, Thin, and Phusion Passenger (to name a
             few!).


             For more details on the Rack integration, see Rails on Rack.


             To use a different server, just install its gem, then use its name for the first parameter to                 :




15 sur 22                                                                                                                                                                     20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                             http://guides.rubyonrails.org/command_line.html




             2.3 The Rails Generation: Generators
             For a good rundown on generators, see Understanding Generators. A lot of its material is presented here.


             Generators are code that generates code. Let’s experiment by building one. Our generator will generate a text file.


             The Rails generator by default looks in these places for available generators, where Rails.root is the root of your Rails application, like /home/foobar/commandsapp:


                    Rails.root/lib/generators
                    Rails.root/vendor/generators
                    Inside any plugin with a directory like “generators” or “rails_generators”
                    ~/.rails/generators
                    Inside any Gem you have installed with a name ending in “_generator”
                    Inside any Gem installed with a “rails_generators” path, and a file ending in “_generator.rb”
                    Finally, the builtin Rails generators (controller, model, mailer, etc.)


             Let’s try the fourth option (in our home directory), which will be easy to clean up later:




             We’ll fill                                    out with:




16 sur 22                                                                                                                                                                        20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line   http://guides.rubyonrails.org/command_line.html




17 sur 22                                                                              20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                        http://guides.rubyonrails.org/command_line.html




             We take whatever args are supplied, save them to an instance variable, and literally copying from the Rails source, implement a       method, which calls
                     with a block, and we:


                   Check there’s a public directory. You bet there is.
                   Run the ERb template called “tutorial.erb”.
                   Save it into “Rails.root/public/tutorial.txt”.
                   Pass in the arguments we saved through the               parameter.


             Next we’ll build the template:




             Then we’ll make sure it got included in the list of available generators:




             SWEET! Now let’s generate some text, yeah!


18 sur 22                                                                                                                                                                   20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                  http://guides.rubyonrails.org/command_line.html




             And the result:




             Tada!


             2.4 Rake is Ruby Make
             Rake is a standalone Ruby utility that replaces the Unix utility ‘make’, and uses a ‘Rakefile’ and      files to build up a list of tasks. In Rails, Rake is used for
             common administration tasks, especially sophisticated ones that build off of each other.


             You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing                 . Each task has a description, and should
             help you find the thing you need.




19 sur 22                                                                                                                                                                             20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                       http://guides.rubyonrails.org/command_line.html




             Let’s take a look at some of these 80 or so rake tasks.

             2.4.1        Database

             The most common tasks of the           Rake namespace are              and          , and it will pay off to try out all of the migration rake tasks (   ,     ,      ,       ).
                                  is useful when troubleshooting, telling you the current version of the database.


             2.4.2         Documentation

             If you want to strip out or rebuild any of the Rails documentation (including this guide!), the       namespace has the tools. Stripping documentation is mainly useful
             for slimming your codebase, like if you’re writing a Rails application for an embedded platform.

             2.4.3          Ruby gems

             You can specify which gems your application uses, and                            will install them for you. Look at your environment.rb to learn how with the config.gem
             directive.


                             will unpack, that is internalize your application’s Gem dependencies by copying the Gem code into your vendor/gems directory. By doing this you
             increase your codebase size, but simplify installation on new hosts by eliminating the need to run                            , or finding and installing the gems your
             application uses.

             2.4.4           Code note enumeration

             These tasks will search through your code for commented lines beginning with “FIXME”, “OPTIMIZE”, “TODO”, or any custom annotation (like XXX) and show you
             them.

             2.4.5           Rails-specific tasks

             In addition to the               task above, you can also unpack the Rails backend specific gems into vendor/rails by calling                                      , to unpack
             the version of Rails you are currently using, or                              to unpack the most recent (cutting, bleeding edge) version.



20 sur 22                                                                                                                                                                                  20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                                       http://guides.rubyonrails.org/command_line.html


             When you have frozen the Rails gems, Rails will prefer to use the code in vendor/rails instead of the system Rails gems. You can “thaw” by running
                                .


             After upgrading Rails, it is useful to run                , which will update your config and scripts directories, and upgrade your Rails-specific javascript (like
             Scriptaculous).

             2.4.6          Rails tests

             A good description of unit testing in Rails is given in A Guide to Testing Rails Applications


             Rails comes with a test suite called Test::Unit. It is through the use of tests that Rails itself is so stable, and the slew of people working on Rails can prove that
             everything works as it should.


             The         namespace helps in running the different tests you will (hopefully!) write.


             2.4.7          Timezones

             You can list all the timezones Rails knows about with                             , which is useful just in day-to-day life.


             2.4.8       Temporary files

             The tmp directory is, like in the *nix /tmp directory, the holding place for temporary files like sessions (if you’re using a file store for files), process id files, and cached
             actions. The           namespace tasks will help you clear them if you need to if they’ve become overgrown, or create them in case of an                   gone awry.


             2.4.9 Miscellaneous Tasks

                            is great for looking at statistics on your code, displaying things like KLOCs (thousands of lines of code) and your code to test ratio.                    will
             give you a psuedo-random key to use for your session secret.                      will list all of your defined routes, which is useful for tracking down routing problems in
             your app, or giving you a good overview of the URLs in an app you’re trying to get familiar with.


             Feedback
             You're encouraged to help in keeping the quality of this guide.


             If you see any typos or factual errors you are confident to patch please clone docrails and push the change yourself. That branch of Rails has public write access.
             Commits are still reviewed, but that happens after you've submitted your contribution. docrails is cross-merged with master periodically.




21 sur 22                                                                                                                                                                                     20/06/2011 13:46
Ruby on Rails Guides: A Guide to The Rails Command Line                                                                                            http://guides.rubyonrails.org/command_line.html


             You may also find incomplete content, or stuff that is not up to date. Please do add any missing documentation for master. Check the Ruby on Rails Guides
             Guidelines guide for style and conventions.


             Issues may also be reported in Github.


             And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome in the rubyonrails-docs mailing list.


                  This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License


                  "Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.




22 sur 22                                                                                                                                                                       20/06/2011 13:46

More Related Content

What's hot

O que há de novo no Rails 3
O que há de novo no Rails 3O que há de novo no Rails 3
O que há de novo no Rails 3Hugo Baraúna
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with LaravelMichael Peacock
 
Apache Manager Table of Contents
Apache Manager Table of ContentsApache Manager Table of Contents
Apache Manager Table of Contentswebhostingguy
 
Ruby on Rails workshop for beginner
Ruby on Rails workshop for beginnerRuby on Rails workshop for beginner
Ruby on Rails workshop for beginnerUmair Amjad
 
Knowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP frameworkKnowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP frameworkBukhori Aqid
 
How to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorialHow to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorialKaty Slemon
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSource Conference
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Dilouar Hossain
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Coursepeter_marklund
 
Rails Engines as a way to Micro services
Rails Engines as a way to Micro servicesRails Engines as a way to Micro services
Rails Engines as a way to Micro servicesLucas Alencar
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsousli07
 

What's hot (16)

Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
O que há de novo no Rails 3
O que há de novo no Rails 3O que há de novo no Rails 3
O que há de novo no Rails 3
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
 
Apache Manager Table of Contents
Apache Manager Table of ContentsApache Manager Table of Contents
Apache Manager Table of Contents
 
Ruby on Rails workshop for beginner
Ruby on Rails workshop for beginnerRuby on Rails workshop for beginner
Ruby on Rails workshop for beginner
 
Laravel Introduction
Laravel IntroductionLaravel Introduction
Laravel Introduction
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
 
Knowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP frameworkKnowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP framework
 
How to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorialHow to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorial
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on Rails
 
Rails onCpanel
Rails onCpanelRails onCpanel
Rails onCpanel
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
Rails Engines as a way to Micro services
Rails Engines as a way to Micro servicesRails Engines as a way to Micro services
Rails Engines as a way to Micro services
 
Why Laravel?
Why Laravel?Why Laravel?
Why Laravel?
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 

Viewers also liked

Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyNikhil Mungel
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in rubyHuy Do
 
Command Line Applications with Ruby
Command Line Applications with RubyCommand Line Applications with Ruby
Command Line Applications with RubyAlexander Merkulov
 
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...Justin Gordon
 
Rails Text Mate Cheats
Rails Text Mate CheatsRails Text Mate Cheats
Rails Text Mate Cheatsdezarrolla
 
Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104Heng-Yi Wu
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Mark Menard
 
Rails 3 generators
Rails 3 generatorsRails 3 generators
Rails 3 generatorsjoshsmoore
 
Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Richard Schneeman
 
Developing cross platform desktop application with Ruby
Developing cross platform desktop application with RubyDeveloping cross platform desktop application with Ruby
Developing cross platform desktop application with RubyAnis Ahmad
 

Viewers also liked (14)

Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
 
Command Line Applications with Ruby
Command Line Applications with RubyCommand Line Applications with Ruby
Command Line Applications with Ruby
 
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
 
Rails Text Mate Cheats
Rails Text Mate CheatsRails Text Mate Cheats
Rails Text Mate Cheats
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
 
Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
 
Rails01
Rails01Rails01
Rails01
 
Ruby on Rails 101
Ruby on Rails 101Ruby on Rails 101
Ruby on Rails 101
 
Rails 3 generators
Rails 3 generatorsRails 3 generators
Rails 3 generators
 
Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3
 
Developing cross platform desktop application with Ruby
Developing cross platform desktop application with RubyDeveloping cross platform desktop application with Ruby
Developing cross platform desktop application with Ruby
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
 

Similar to Railsguide

Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on railspmashchak
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsiradarji
 
Úvod do Ruby on Rails
Úvod do Ruby on RailsÚvod do Ruby on Rails
Úvod do Ruby on RailsKarel Minarik
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsAlessandro DS
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsanides
 
Ruby on Rails : First Mile
Ruby on Rails : First MileRuby on Rails : First Mile
Ruby on Rails : First MileGourab Mitra
 
The Birth and Evolution of Ruby on Rails
The Birth and Evolution of Ruby on RailsThe Birth and Evolution of Ruby on Rails
The Birth and Evolution of Ruby on Railscompany
 
Ruby on rails vs asp.net mvc
Ruby on rails vs asp.net mvcRuby on rails vs asp.net mvc
Ruby on rails vs asp.net mvcUmar Ali
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web DevelopmentSonia Simi
 
From .Net to Rails, A developers story
From .Net to Rails, A developers storyFrom .Net to Rails, A developers story
From .Net to Rails, A developers storypythonandchips
 
An introduction to Rails 3
An introduction to Rails 3An introduction to Rails 3
An introduction to Rails 3Blazing Cloud
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to railsEvgeniy Hinyuk
 
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukPivorak MeetUp
 
A Tour of Ruby On Rails
A Tour of Ruby On RailsA Tour of Ruby On Rails
A Tour of Ruby On RailsDavid Keener
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsManoj Kumar
 

Similar to Railsguide (20)

Learning Rails
Learning RailsLearning Rails
Learning Rails
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Ruby Rails Web Development.pdf
Ruby Rails Web Development.pdfRuby Rails Web Development.pdf
Ruby Rails Web Development.pdf
 
Úvod do Ruby on Rails
Úvod do Ruby on RailsÚvod do Ruby on Rails
Úvod do Ruby on Rails
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Ruby on Rails : First Mile
Ruby on Rails : First MileRuby on Rails : First Mile
Ruby on Rails : First Mile
 
The Birth and Evolution of Ruby on Rails
The Birth and Evolution of Ruby on RailsThe Birth and Evolution of Ruby on Rails
The Birth and Evolution of Ruby on Rails
 
Ruby on rails vs asp.net mvc
Ruby on rails vs asp.net mvcRuby on rails vs asp.net mvc
Ruby on rails vs asp.net mvc
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
 
From .Net to Rails, A developers story
From .Net to Rails, A developers storyFrom .Net to Rails, A developers story
From .Net to Rails, A developers story
 
An introduction to Rails 3
An introduction to Rails 3An introduction to Rails 3
An introduction to Rails 3
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
 
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
 
A Tour of Ruby On Rails
A Tour of Ruby On RailsA Tour of Ruby On Rails
A Tour of Ruby On Rails
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
Intro to Rails
Intro to RailsIntro to Rails
Intro to Rails
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Rails 6 features
Rails 6 featuresRails 6 features
Rails 6 features
 

Recently uploaded

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Railsguide

  • 1. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html More at rubyonrails.org: Overview | Download | Deploy | Code | Screencasts | Documentation | Ecosystem | Community | Blog A Guide to The Rails Command Line Rails comes with every command line tool you’ll need to Create a Rails application Generate models, controllers, database migrations, and unit tests Start a development server Mess with objects through an interactive shell Profile and benchmark your new creation This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide. Chapters 1. Command Line Basics 1 sur 22 20/06/2011 13:46
  • 2. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html 2. The Rails Advanced Command Line Rails with Databases and SCM with Different Backends The Rails Generation: Generators Rake is Ruby Make This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails. 1 Command Line Basics There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you’ll probably use them are: Let’s create a simple Rails application to step through each of these commands in context. 1.1 The first thing we’ll want to do is create a new Rails application by running the command after installing Rails. You know you need the rails gem installed by typing first, if you don’t have this installed, follow the instructions in the Rails 3 2 sur 22 20/06/2011 13:46
  • 3. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html Release Notes Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You’ve got the entire Rails directory structure now with all the code you need to run our simple application right out of the box. This output will seem very familiar when we get to the command. Creepy foreshadowing! 1.2 Let’s try it! The command launches a small web server named WEBrick which comes bundled with Ruby. You’ll use this any time you want to view your work through a web browser. 3 sur 22 20/06/2011 13:46
  • 4. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html WEBrick isn’t your only option for serving Rails. We’ll get to that in a later section. Without any prodding of any kind, will run our new shiny Rails app: With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open http://localhost:3000, you will see a basic rails app running. 1.3 The command uses templates to create a whole lot of things. You can always find out what’s available by running by itself. Let’s do that: 4 sur 22 20/06/2011 13:46
  • 5. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html You can install more generators through generator gems, portions of plugins you’ll undoubtedly install, and you can even create your own! Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend time writing. That’s what we have computers for. Let’s make our own controller with the controller generator. But what command should we use? Let’s ask the generator: All Rails console utilities have help text. As with most *NIX utilities, you can try adding or to the end, for example . 5 sur 22 20/06/2011 13:46
  • 6. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html The controller generator is expecting parameters in the form of . Let’s make a controller with an action of hello, which will say something nice to us. What all did this generate? It made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file. Check out the controller and modify it a little (in ):ma 6 sur 22 20/06/2011 13:46
  • 7. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html Then the view, to display our message (in ): Deal. Go check it out in your browser. Fire up your server. Remember? at the root of your Rails application should do it. Make sure that you do not have any “tilde backup” files in , or else WEBrick will not show the expected output. This seems to be a bug in Rails 2.3.0. The URL will be http://localhost:3000/greetings/hello. 7 sur 22 20/06/2011 13:46
  • 8. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit the index action of that controller. Rails comes with a generator for data models too: For a list of available field types, refer to the API documentation for the column method for the class. But instead of generating a model directly (which we’ll be doing later), let’s set up a scaffold. A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above. We will set up a simple resource called “HighScore” that will keep track of our highest score on video games we play. 8 sur 22 20/06/2011 13:46
  • 9. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html 9 sur 22 20/06/2011 13:46
  • 10. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html The generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the table and fields), takes care of the route for the resource, and new tests for everything. The migration requires that we migrate, that is, run some Ruby code (living in that ) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the command. We’ll talk more about Rake in-depth in a little while. Let’s talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We’ll make one in a moment. Let’s see the interface Rails created for us. Go to your browser and open http://localhost:3000/high_scores, now we can create new high scores (55,160 on Space Invaders!) 1.4 The command lets you interact with your Rails application from the command line. On the underside, uses IRB, so if you’ve ever used it, 10 sur 22 20/06/2011 13:46
  • 11. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html you’ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website. 1.5 figures out which database you’re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3. 1.6 The command simplifies plugin management; think a miniature version of the Gem utility. Let’s walk through installing a plugin. You can call the sub-command , which sifts through repositories looking for plugins, or call to add a specific repository of plugins, or you can specify the plugin location directly. Let’s say you’re creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn’t it be great if we could override the behavior of a model to never actually take its record out of the database, but instead, just set a field? There is such a thing! The plugin we’re installing is called , and it lets models implement a column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out “deleted” things. 1.7 runs Ruby code in the context of Rails non-interactively. For instance: 11 sur 22 20/06/2011 13:46
  • 12. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html 1.8 Think of as the opposite of . It’ll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times! 12 sur 22 20/06/2011 13:46
  • 13. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html 1.9 Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application’s folder, the current Rails environment name, your app’s database adapter, and schema version! is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation. 2 The Rails Advanced Command Line The more advanced uses of the command line are focused around finding useful (even surprising at times) options in the utilities, and fitting utilities to your needs and specific work flow. Listed here are some tricks up Rails’ sleeve. 2.1 Rails with Databases and SCM When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes. Let’s see what a option and a option will do for us: 13 sur 22 20/06/2011 13:46
  • 14. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html We had to create the gitapp directory and initialize an empty git repository before Rails would add files it created to our repository. Let’s see what it put in our database configuration: 14 sur 22 20/06/2011 13:46
  • 15. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options is that you have to make your application’s directory first, then initialize your SCM, then you can run the command to generate the basis of your app. 2.2 with Different Backends Many people have created a large number different web servers in Ruby, and many of them can be used to run Rails. Since version 2.3, Rails uses Rack to serve its webpages, which means that any webserver that implements a Rack handler can be used. This includes WEBrick, Mongrel, Thin, and Phusion Passenger (to name a few!). For more details on the Rack integration, see Rails on Rack. To use a different server, just install its gem, then use its name for the first parameter to : 15 sur 22 20/06/2011 13:46
  • 16. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html 2.3 The Rails Generation: Generators For a good rundown on generators, see Understanding Generators. A lot of its material is presented here. Generators are code that generates code. Let’s experiment by building one. Our generator will generate a text file. The Rails generator by default looks in these places for available generators, where Rails.root is the root of your Rails application, like /home/foobar/commandsapp: Rails.root/lib/generators Rails.root/vendor/generators Inside any plugin with a directory like “generators” or “rails_generators” ~/.rails/generators Inside any Gem you have installed with a name ending in “_generator” Inside any Gem installed with a “rails_generators” path, and a file ending in “_generator.rb” Finally, the builtin Rails generators (controller, model, mailer, etc.) Let’s try the fourth option (in our home directory), which will be easy to clean up later: We’ll fill out with: 16 sur 22 20/06/2011 13:46
  • 17. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html 17 sur 22 20/06/2011 13:46
  • 18. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html We take whatever args are supplied, save them to an instance variable, and literally copying from the Rails source, implement a method, which calls with a block, and we: Check there’s a public directory. You bet there is. Run the ERb template called “tutorial.erb”. Save it into “Rails.root/public/tutorial.txt”. Pass in the arguments we saved through the parameter. Next we’ll build the template: Then we’ll make sure it got included in the list of available generators: SWEET! Now let’s generate some text, yeah! 18 sur 22 20/06/2011 13:46
  • 19. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html And the result: Tada! 2.4 Rake is Ruby Make Rake is a standalone Ruby utility that replaces the Unix utility ‘make’, and uses a ‘Rakefile’ and files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other. You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing . Each task has a description, and should help you find the thing you need. 19 sur 22 20/06/2011 13:46
  • 20. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html Let’s take a look at some of these 80 or so rake tasks. 2.4.1 Database The most common tasks of the Rake namespace are and , and it will pay off to try out all of the migration rake tasks ( , , , ). is useful when troubleshooting, telling you the current version of the database. 2.4.2 Documentation If you want to strip out or rebuild any of the Rails documentation (including this guide!), the namespace has the tools. Stripping documentation is mainly useful for slimming your codebase, like if you’re writing a Rails application for an embedded platform. 2.4.3 Ruby gems You can specify which gems your application uses, and will install them for you. Look at your environment.rb to learn how with the config.gem directive. will unpack, that is internalize your application’s Gem dependencies by copying the Gem code into your vendor/gems directory. By doing this you increase your codebase size, but simplify installation on new hosts by eliminating the need to run , or finding and installing the gems your application uses. 2.4.4 Code note enumeration These tasks will search through your code for commented lines beginning with “FIXME”, “OPTIMIZE”, “TODO”, or any custom annotation (like XXX) and show you them. 2.4.5 Rails-specific tasks In addition to the task above, you can also unpack the Rails backend specific gems into vendor/rails by calling , to unpack the version of Rails you are currently using, or to unpack the most recent (cutting, bleeding edge) version. 20 sur 22 20/06/2011 13:46
  • 21. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html When you have frozen the Rails gems, Rails will prefer to use the code in vendor/rails instead of the system Rails gems. You can “thaw” by running . After upgrading Rails, it is useful to run , which will update your config and scripts directories, and upgrade your Rails-specific javascript (like Scriptaculous). 2.4.6 Rails tests A good description of unit testing in Rails is given in A Guide to Testing Rails Applications Rails comes with a test suite called Test::Unit. It is through the use of tests that Rails itself is so stable, and the slew of people working on Rails can prove that everything works as it should. The namespace helps in running the different tests you will (hopefully!) write. 2.4.7 Timezones You can list all the timezones Rails knows about with , which is useful just in day-to-day life. 2.4.8 Temporary files The tmp directory is, like in the *nix /tmp directory, the holding place for temporary files like sessions (if you’re using a file store for files), process id files, and cached actions. The namespace tasks will help you clear them if you need to if they’ve become overgrown, or create them in case of an gone awry. 2.4.9 Miscellaneous Tasks is great for looking at statistics on your code, displaying things like KLOCs (thousands of lines of code) and your code to test ratio. will give you a psuedo-random key to use for your session secret. will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you’re trying to get familiar with. Feedback You're encouraged to help in keeping the quality of this guide. If you see any typos or factual errors you are confident to patch please clone docrails and push the change yourself. That branch of Rails has public write access. Commits are still reviewed, but that happens after you've submitted your contribution. docrails is cross-merged with master periodically. 21 sur 22 20/06/2011 13:46
  • 22. Ruby on Rails Guides: A Guide to The Rails Command Line http://guides.rubyonrails.org/command_line.html You may also find incomplete content, or stuff that is not up to date. Please do add any missing documentation for master. Check the Ruby on Rails Guides Guidelines guide for style and conventions. Issues may also be reported in Github. And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome in the rubyonrails-docs mailing list. This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License "Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved. 22 sur 22 20/06/2011 13:46