/railsTips

A repository to hold all ruby, ruby on rails tips and tricks I find while studying

Ruby on Rails Tips

A repository to hold all ruby, ruby on rails tips and tricks I find while studying!

Ruby

For ruby basic tips, checkout ruby.md in this repository

Rails

New project

$ rails new appName
$ rails new appName --database=postgresql

Files in rails project

├── Gemfile
├── Gemfile.lock
├── README.rdoc
├── Rakefile
├── app
│   ├── assets
│   ├── controllers   # Controller
│   ├── helpers
│   ├── mailers
│   ├── models        # Model
│   └── views         # View
├── bin
├── config
├── config.ru
├── db
├── lib
├── log
├── public
├── test
├── tmp
└── vendor

Routing

Inside /config/routes.rb

root 'static#top'

Use only to use specific action(s)

resources :books, only: [:index, :show]

Scope Routing Rules

get 'books/new' => 'books#new'
get 'books/edit/:id' => 'books#edit'
get 'books/undo/:id' => 'books#undo'

DRY 👇

scope controller: :books do
  get 'books/new' => :new
  get 'books/edit/:id' => :edit
  get 'books/undo/:id' => :undo
end

DRY 👇

scope path: '/books', controller: :books do
  get 'new' => :new
  get 'edit/:id' => :edit
  get 'undo/:id' => :undo
end

Even more syntactic

scope controller: :books do

👇

scope :books do

👇

controller :books do

Path Prefix

scope path: '/books' do
scope '/books/' do

# nested implied prefixed paths

scope :books, :archived do

Namespaces

Will bundle module, name prefix, and path prefix settings into one declaration

namespace :admin do
  resources :books
end

namespace :books, :controller => :books do
  get 'new' => :new
  get 'edit/:id' => :edit
  post 'undo/:id' => :undo
end

Model

Controller

Namespace for controllers

If you want to add a namespace to controllers, categorize the files with folder

Add base controller for authentication

View

HTML

  • ERB
  • haml
  • slim

CSS

  • Sass
  • Less

Javascript

  • coffee