Ruby on Rails Guides_ a Guide to the Rails Command Line

July 1, 2016 | Author: Brian Morales | Category: N/A
Share Embed Donate


Short Description

Download Ruby on Rails Guides_ a Guide to the Rails Command Line...

Description

3/21/12

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 Line Rails comes with every command line tool you ll need to C 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

i h objec

e h o gh an in e ac i e hell

P ofile and benchma k o

ne

c ea ion

Chap e 1. Command Line Ba ic rails new rails server rails generate rails console rails dbconsole rails plugin rails runner rails destroy 2. Rake about assets db doc notes routes test tmp Miscellaneous 3. The Rail Ad anced Command Line Rails with Databases and SCM serverwith Different Backends

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 ic guides.rub onrails.org/command_line.html

1/13

3/21/12

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: rails console rails server rake rails generate rails dbconsole rails new app_name Let s create a simple Rails application to step through each of these commands in context.

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

You can install the rails gem by typing gem install rails, if you don t have it already. Follow the instructions in the Rail 3 Relea e No e

$ rails new commandsapp create create README.rdoc create .gitignore create Rakefile create config.ru create Gemfile create app ... create tmp/cache create tmp/pids create vendor/plugins create vendor/plugins/.gitkeep

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 rails server The rails servercommand launches a small web server named WEBrick which comes bundled with Ruby. You ll use this any time you want 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, rails serverwill run our new shiny Rails app:

$ cd commandsapp $ rails server = Booting WEBrick = Rails 3.1.0 application starting in development on http://0.0.0.0:3000 guides.rub onrails.org/command_line.html

2/13

3/21/12

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

= Call with -d to detach = Ctrl-C to shutdown server [2010-04-18 03:20:33] INFO WEBrick 1.3.1 [2010-04-18 03:20:33] INFO ruby 1.8.7 (2010-01-10) [x86_64-linux] [2010-04-18 03:20:33] INFO WEBrick::HTTPServer#start: pid=26086 port=3000

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: rails s. The server can be run on a different port using the -poption. The default development environment can be changed using -e.

$ rails server -e production -p 4000

The -boption binds Rails to the specified ip, by default it is 0.0.0.0. You can run a server as a daemon by passing a -doption.

1.3 rails generate The rails generatecommand uses templates to create a whole lot of things. Running rails generateby itself gives a list of available generators: You can also use the alias ā€œgā€ to invoke the generator command: rails g.

$ rails generate Usage: rails generate GENERATOR [args] [options] ... ... Please choose a generator below. Rails: controller generator ... ...

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 --helpor -hto the end, for example rails server -help.

$ rails generate controller Usage: rails generate controller NAME [action action] [options] ... ... Example: rails generate controller CreditCard open debit credit close guides.rub onrails.org/command_line.html

3/13

3/21/12

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

Credit card controller with URLs like /credit_card/debit. Controller: app/controllers/credit_card_controller.rb Views:

app/views/credit_card/debit.html.erb [...]

Helper:

app/helpers/credit_card_helper.rb

Test:

test/functional/credit_card_controller_test.rb

Modules Example: rails generate controller 'admin/credit_card' suspend late_fee

Credit card admin controller with URLs like /admin/credit_card/suspend. Controller: app/controllers/admin/credit_card_controller.rb Views:

app/views/admin/credit_card/debit.html.erb [...]

Helper:

app/helpers/admin/credit_card_helper.rb

Test:

test/functional/admin/credit_card_controller_test.rb

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

$ rails generate controller Greetings hello create app/controllers/greetings_controller.rb route get "greetings/hello" invoke erb create

app/views/greetings

create

app/views/greetings/hello.html.erb

invoke test_unit create

test/functional/greetings_controller_test.rb

invoke helper create

app/helpers/greetings_helper.rb

invoke

test_unit

create

test/unit/helpers/greetings_helper_test.rb

invoke assets create

app/assets/javascripts/greetings.js

invoke

css

create

app/assets/stylesheets/greetings.css

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 app/controllers/greetings_controller.rb):

guides.rub onrails.org/command_line.html

4/13

3/21/12

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

class GreetingsController < ApplicationController def hello message = Hello, how are you today? end end

Then the view, to display our message (in app/views/greetings/hello.html.erb):

< h1 A Greeting for You!
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF