Rails is a language particularly known for its rapid development and shortcut commands that make development of applications and its modules fast and easy. Today I am going to tell you about few most basic commands of rails:
1. rails new:
This command is the first command for rails that is used for creating applications in Ruby on Rails. Once you hit this command it creates a full fledged rails application with full structured rails app.
rails new sample_app
It will give output like:
$ rails new sample_app
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
...
create tmp/cache
...
run bundle install
2. rails server:
This command starts the WEBrick server that starts the application. By default it runs the application on port number 3000 in development mode. you can change the port number and mode by providing it with -p [For port number] and -e [For environment]. The syntax for this is
rails server
It will give output like:
=> Booting WEBrick
=> Rails 4.2.5.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-06-02 15:21:21] INFO WEBrick 1.3.1
[2016-06-02 15:21:21] INFO ruby 2.2.1 (2015-02-26) [x86_64-linux]
[2016-06-02 15:21:21] INFO WEBrick::HTTPServer#start: pid=8143 port=3000
## For running app with port number and environment you can run this command like:
rails server -p PORT_NUMBER -e ENVIRONMENT
3. rails generate:
This is the command that acts as a generator for creating different types of files in Ruby on Rails application like Models, Controllers etc.
Syntax:
rails generate GENERATOR [args] [options]
To generate a model use the following:
rails generate model ModelName column:datatype column:datatype [...]
$ rails g model User name:string email:string
Running via Spring preloader in process 10196
invoke active_record
create db/migrate/20160602115029_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
To generate a scaffold use the following:
rails generate scaffold ModelName column:datatype column:datatype [...]
$ rails generate scaffold User name:string email:string
Running via Spring preloader in process 10160
invoke active_record
create db/migrate/20160602114830_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
invoke resource_route
route resources :users
invoke scaffold_controller
create app/controllers/users_controller.rb
invoke erb
create app/views/users
create app/views/users/index.html.erb
create app/views/users/edit.html.erb
create app/views/users/show.html.erb
create app/views/users/new.html.erb
create app/views/users/_form.html.erb
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
invoke jbuilder
create app/views/users/index.json.jbuilder
create app/views/users/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/users.coffee
invoke scss
create app/assets/stylesheets/users.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss
To generate a controller use the following:
rails generate controller ControllerName [actions]
$ rails g controller users
Running via Spring preloader in process 10237
create app/controllers/users_controller.rb
invoke erb
create app/views/users
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/users.coffee
invoke scss
create app/assets/stylesheets/users.scss
4. rails console:
This command lets you interact with the application and its database through console. It is very helpful in debugging the application. Application methods can also be called from this console.
$ rails c
Running via Spring preloader in process 10313
Loading development environment (Rails 4.2.5.1)
2.2.1 :001 >
5. rails destroy:
This command is used for destroying the things already generated using rails command like if you have generated model User using rails g then you can remove it using rails d.
$ rails g model User
Running via Spring preloader in process 10509
invoke active_record
create db/migrate/20160602121026_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
$ rails d model User
Running via Spring preloader in process 10529
invoke active_record
remove db/migrate/20160602121026_create_users.rb
remove app/models/user.rb
invoke test_unit
remove test/models/user_test.rb
remove test/fixtures/users.yml
Hope you enjoyed reading this. Will talk about some more command line features of rails soon.
0 Comment(s)