Sunday, February 5, 2012

Rails cheat sheet

#  create a new project (the -T flag means, don't make the test directory)
    rails new new_project_name -T

#  to include 'gems' to be installed for your app
    modify the Gemfile
    bundle update
    bundle install

#  do an install with a flag to skip some things
    bundle install --without production

#  create a resource, called "User" with attributes "name" and "email" of type string
    This will create all the MVC parts.
    rails generate scaffold User name:string email:string

#  The user Model is defined in this file.
#  This is similar to a C++ class definition (it even inherits from something, ActiveRecord::Base)
#  You can further tailor your model by adding validation constraints or
#  or create relationships between this model and others
    app/models/user.rb

#  The user Controller is defined in this file.
#  This looks like a list of event handlers that can affect your users model or view.
    app/controllers/users_controller.rb

#  The user View is defined in this file.
#  This gives info on how to display the page.
    app/views/users/index.html.erb

#  If you don't generate with a scaffold, this creates just a model, called "Model_name" with attribute1 and attribute2 of type string without the controller or view.
    rails generate model Model_name attribute1:string attribute2:string

#  If you don't generate with a scaffold, this creates just a controller, called "controller_name" that will control page "page_name"
    rails generate controller controller_name page_name

#  update the database structure after generate controller or generate user ...
    bundle exec rake db:migrate

#  undo a database update (the previous command)
    bundle exec rake db:rollback

#  get more help for database tasks
    bundle exec rake -T db

#  turn on the rails server
    rails s
    or (rails server)

#  start interactive rails environment
    rails console

#  location of resource pages [after generating the user resource and adding it to our database]
    /users   -> list of users
    /users/1  -> list of user with id == 1
    /users/new  -> place to make new users
    /users/1/edit  -> place to edit user with id == 1

#  create an integration test
    rails generate integration_test test_name
    - creates a test file under test_name_spec.rb

#  run test files (that are in directory spec/)
    bundle exec rspec spec/

#   create rspec tests
#   this creates files like spec/spec_helper.rb
     rails generate rspec:install

No comments:

Post a Comment