rails generate controller code example

Example 1: rails g model

$ rails generate model Article title:string text:text

Example 2: how to generate a controller in rails

$ rails generate controller Greetings hello
	 
     # Below are the files this generator creates
     # hello would create a view file
     # to create the controller without creating the view file enter:
     # rails generate controller Greetings
     
     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/controllers/greetings_controller_test.rb
     invoke  helper
     create    app/helpers/greetings_helper.rb
     invoke    test_unit
     invoke  assets
     invoke    scss
     create      app/assets/stylesheets/greetings.scss

Example 3: rails controller generator

$ rails generate controller Welcome index

Example 4: rails command line

rails new app_name #creates a new rails application
rails server or rails s #runs the rails server
rails generate or rails g #gives a list of available generators 
rails console or rails c #opens up the rails console that allows you 
						 # to interact with your rails application
rails destroy or rails d #allows you to destroy what was previously created
# ex: rails g model Oops - creates
# ex: rails d model Oops - deletes what was created
rails about #gives info about your ruby application including the version, etc.
rails -T #opens up the rails tasks list to give you info on what you can run.
rails db: #most common command, allows you to run create or migrate
#ex: rails db:migrate - runs the migration
#ex: rails db:seed - seeds your DB from what was added in your seed file

Tags:

Ruby Example