Ruby on Rails Guides_ a Guide to the Rails Command Line

  • View

  • Download

Embed Size (px)

Text of Ruby on Rails Guides_ a Guide to the Rails Command Line


Rub on Rails Guides: A Guide to The Rails Command Line

Mo e a

b on ail .o g: O e ie

| Do nload | Deplo | Code | Sc eenca

| Doc men a ion | Eco

em | Comm ni

| Blog

A G ide o The Rail Command LineRails comes with every command line tool you ll need toC ea e a Rail applica ion Gene a e model , con olle , da aba e mig a ion , and ni e S a a de elopmen E pe imen e e h o gh an in e ac i e hell ne c ea ion

i h objec

P ofile and benchma k o

Chap e1. Command Line Ba ic risnw al e rissre al evr risgnrt al eeae riscnoe al osl risdcnoe al bosl rispui al lgn risrne al unr risdsry al eto 2. Rake aot bu ast ses d b dc o nts oe rue ots ts et tp m Miscellaneous 3. The Rail Ad anced Command Line Rails with Databases and SCM s r e with Different Backends evr

This tutorial assumes you have basic Rails knowledge from reading the Ge ing S a ed

i h Rail G ide.

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 Ba icguides.rub 1/13


Rub on Rails Guides: A Guide to The Rails Command Line

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: riscnoe al osl rissre al evr rk ae risgnrt al eeae risdcnoe al bosl risnwapnm al e p_ae Let s create a simple Rails application to step through each of these commands in context.

1.1 r i s n w al eThe first thing we ll want to do is create a new Rails application by running the r i s n wcommand after installing Rails. al e

You can install the rails gem by typing g m i s a l r i s if you don t have it already. Follow the instructions in the Rail 3 e ntl al, Relea e No e

$risnwcmadap al e omnsp cet rae cet RAM.dc rae EDEro cet .iinr rae gtgoe cet Rkfl rae aeie cet cni.u rae ofgr cet Gmie rae efl cet ap rae p .. . cet tpcce rae m/ah cet tppd rae m/is cet vno/lgn rae edrpuis cet vno/lgn/gtep rae edrpuis.ike

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.

1.2 r i s s r e al evrThe r i s s r e command launches a small web server named WEBrick which comes bundled with Ruby. You ll use this any time you want al evr to access your application through a web browser. WEBrick isn t your only option for serving Rails. We ll get to that la e . With no further work, r i s s r e will run our new shiny Rails app: al evr

$c cmadap d omnsp $rissre al evr = BoigWBik otn Erc = Ris310apiainsatn i dvlpeto ht:/...:00 al .. plcto trig n eeomn n tp/000030guides.rub 2/13


Rub on Rails Guides: A Guide to The Rails Command Line

= Cl wt - t dtc al ih d o eah = Cr- t sudw sre tlC o hton evr [000-80:03]IF WBik131 21-41 32:3 NO Erc .. [000-80:03]IF rb 187(000-0 [8_4lnx 21-41 32:3 NO uy .. 21-11) x66-iu] [000-80:03]IF WBik:TPevrsat pd206pr=00 21-41 32:3 NO Erc:HTSre#tr: i=68 ot30

With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open h p://localho :3000, you will see a basic Rails app running. You can also use the alias s to start the server: r i s s al . The server can be run on a different port using the - option. The default development environment can be changed using - . p e

$rissre - pouto - 40 al evr e rdcin p 00

The - option binds Rails to the specified ip, by default it is You can run a server as a daemon by passing a - option. b d

1.3 r i s g n r t al eeaeThe r i s g n r t command uses templates to create a whole lot of things. Running r i s g n r t by itself gives a list of available al eeae al eeae generators: You can also use the alias g to invoke the generator command: r i s g al .

$risgnrt al eeae Uae risgnrt GNRTR[rs [pin] sg: al eeae EEAO ag] otos .. . .. . Pes cos agnrtrblw lae hoe eeao eo. Ris al: cnrle otolr gnrtr eeao .. . .. .

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 boile pla e code, code that is necessary for the app to work. 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 - h l or - to the end, for example r i s s r e -ep h al evr hl. ep

$risgnrt cnrle al eeae otolr Uae risgnrt cnrle NM [cinato][pin] sg: al eeae otolr AE ato cin otos .. . .. . Eape xml: risgnrt cnrle CeiCr oe dbtcei coe al eeae otolr rdtad pn ei rdt lsguides.rub 3/13


Rub on Rails Guides: A Guide to The Rails Command Line

Cei cr cnrle wt UL lk /rdtcr/ei. rdt ad otolr ih Rs ie cei_addbt Cnrle:apcnrlescei_adcnrle.b otolr p/otolr/rdtcr_otolrr Ves iw: Hle: epr Ts: et apvescei_addbthm.r [.] p/iw/rdtcr/ei.tleb .. aphlescei_adhle.b p/epr/rdtcr_eprr ts/ucinlcei_adcnrle_etr etfntoa/rdtcr_otolrts.b

MdlsEape oue xml: risgnrt cnrle 'di/rdtcr'ssedlt_e al eeae otolr amncei_ad upn aefe

Cei cr amncnrle wt UL lk /di/rdtcr/upn. rdt ad di otolr ih Rs ie amncei_adssed Cnrle:apcnrlesamncei_adcnrle.b otolr p/otolr/di/rdtcr_otolrr Ves iw: Hle: epr Ts: et apvesamncei_addbthm.r [.] p/iw/di/rdtcr/ei.tleb .. aphlesamncei_adhle.b p/epr/di/rdtcr_eprr ts/ucinlamncei_adcnrle_etr etfntoa/di/rdtcr_otolrts.b

The controller generator is expecting parameters in the form of g n r t c n r l e C n r l e N m a t o 1 a t o 2 Let s make a eeae otolr otolrae cin cin. G e t n scontroller with an action of hello, which will say something nice to us. reig

$risgnrt cnrle Getnshlo al eeae otolr reig el cet apcnrlesgetnscnrle.b rae p/otolr/reig_otolrr rue gt"reig/el" ot e getnshlo ivk eb noe r cet rae cet rae apvesgetns p/iw/reig apvesgetnshlohm.r p/iw/reig/el.tleb

ivk ts_nt noe etui cet rae ts/ucinlgetnscnrle_etr etfntoa/reig_otolrts.b

ivk hle noe epr cet rae ivk noe cet rae aphlesgetnshle.b p/epr/reig_eprr ts_nt etui ts/nthlesgetnshle_etr etui/epr/reig_eprts.b

ivk ast noe ses cet rae ivk noe cet rae apast/aacit/reig.s p/sesjvsrpsgetnsj cs s apast/tlset/reig.s p/sessyehesgetnscs

What all did this generate? It made sure a bunch of directories were in our application, and created a controller file, a view file, a functional test file, a helper for the view, a javascript file and a stylesheet file. Check out the controller and modify it a little (in a p c n r l e s g e t n s c n r l e . b p / o t o l r / r e i g _ o t o l r r ):




Rub on Rails Guides: A Guide to The Rails Command Line

cas ls Getnsotolr