Skip to content

Instantly share code, notes, and snippets.

@chilianubogdan
Forked from mdang/RAILS_CHEATSHEET.md
Created October 12, 2020 03:24
Show Gist options
  • Save chilianubogdan/a7fa15dbc40a5f49f5150fab9c91d59c to your computer and use it in GitHub Desktop.
Save chilianubogdan/a7fa15dbc40a5f49f5150fab9c91d59c to your computer and use it in GitHub Desktop.

Revisions

  1. Mike Dang revised this gist Mar 9, 2016. No changes.
  2. Mike Dang revised this gist Mar 9, 2016. 1 changed file with 20 additions and 33 deletions.
    53 changes: 20 additions & 33 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -304,41 +304,10 @@ Reference: [http://guides.rubyonrails.org/asset_pipeline.html](http://guides.rub

    ### Form Helpers

    Create a basic form that posts to itself

    ```html
    <%= form_tag do %>
    Form contents
    <% end %>
    ```
    ```html
    <form accept-charset="UTF-8" action="/" method="post">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="authenticity_token" type="hidden" value="J7CBxfHalt49OSHp27hblqK20c9PgwJ108nDHX/8Cts=" />
    Form contents
    </form>
    ```

    Create a form with a custom action and method

    ```rb
    <%= form_tag("/search", method: "get") do %>
    <%= label_tag(:q, "Search for:") %>
    <%= text_field_tag(:q) %>
    <%= submit_tag("Search") %>
    <% end %>
    ```
    ```html
    <form accept-charset="UTF-8" action="/search" method="get">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <label for="q">Search for:</label>
    <input id="q" name="q" type="text" />
    <input name="commit" type="submit" value="Search" />
    </form>
    ```

    Bind a form to a model for creating/updating a resource

    **Use this method if you're using strong params to protect against mass assignment**

    ```rb
    # app/controllers/photos_controller.rb
    def new
    @@ -362,4 +331,22 @@ end
    </form>
    ```

    Create a form with a custom action and method

    ```rb
    <%= form_tag("/search", method: "get") do %>
    <%= label_tag(:q, "Search for:") %>
    <%= text_field_tag(:q) %>
    <%= submit_tag("Search") %>
    <% end %>
    ```
    ```html
    <form accept-charset="UTF-8" action="/search" method="get">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <label for="q">Search for:</label>
    <input id="q" name="q" type="text" />
    <input name="commit" type="submit" value="Search" />
    </form>
    ```

    Reference: [http://guides.rubyonrails.org/form_helpers.html](http://guides.rubyonrails.org/form_helpers.html)
  3. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -56,6 +56,8 @@ Automagically create all the routes for a RESTful resource
    resources :photos
    ```

    <img src="https://cloud.githubusercontent.com/assets/25366/8561247/75b73966-24d7-11e5-896a-06506648c4fe.png">

    HTTP Verb | Path | Controller#Action | Used for
    --------- | ---- | ----------------- | -------
    GET | /photos | photos#index | display a list of all photos
  4. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -206,6 +206,8 @@ end

    ### Scaffolding

    Scaffolding is great for prototypes but don't rely too heavily on it: http://stackoverflow.com/a/25140503

    ```
    $ rails g scaffold Photo path:string caption:text
    $ rake db:migrate
  5. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,7 @@ get 'photos/show'

    # The above is the same as:
    get 'photos/show', :to 'photos#show'
    get 'photos/show' => 'photos#show'
    ```

    Automagically create all the routes for a RESTful resource
  6. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    ## Ruby on Rails Cheatsheet

    ### Architecture

    <img src="https://raw.githubusercontent.com/mdang/resources/master/ruby/rails/rails_architecture.png">

    ### Create a new application
  7. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    ## Ruby on Rails Cheatsheet

    ### Architecture

    <img src="https://raw.githubusercontent.com/mdang/resources/master/ruby/rails/rails_architecture.png">

    ### Create a new application
  8. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    ### Architecture

    <img src="https://ga-students.slack.com/files/mattduncan/F0JHRFESK/screen_shot_2016-01-12_at_10.23.03_am.png">
    <img src="https://raw.githubusercontent.com/mdang/resources/master/ruby/rails/rails_architecture.png">

    ### Create a new application

  9. Mike Dang revised this gist Mar 7, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    ## Ruby on Rails Cheatsheet

    ### Architecture

    <img src="https://ga-students.slack.com/files/mattduncan/F0JHRFESK/screen_shot_2016-01-12_at_10.23.03_am.png">

    ### Create a new application

    Install the Rails gem if you haven't done so before
  10. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -337,7 +337,7 @@ def new
    end
    ```
    ```rb
    <!-- ERB view -->
    # ERB view
    <%= form_for @photo, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
    <%= f.text_field :path %>
    <%= f.text_area :caption, size: "60x12" %>
  11. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -328,7 +328,7 @@ Create a form with a custom action and method
    </form>
    ```

    Bind a form to a model
    Bind a form to a model for creating/updating a resource

    ```rb
    # app/controllers/photos_controller.rb
    @@ -337,13 +337,15 @@ def new
    end
    ```
    ```rb
    <!-- ERB view -->
    <%= form_for @photo, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
    <%= f.text_field :path %>
    <%= f.text_area :caption, size: "60x12" %>
    <%= f.submit "Create" %>
    <% end %>
    ```
    ```html
    <!-- HTML output -->
    <form accept-charset="UTF-8" action="/photos/create" method="post" class="nifty_form">
    <input id="photos_path" name="photo[path]" type="text" />
    <textarea id="photos_caption" name="photo[caption]" cols="60" rows="12"></textarea>
  12. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -272,7 +272,7 @@ DELETE | /photos/:id | photos#destroy | photo_path(:id)
    ### Asset Pipeline
    Access images in the `public/assets/images` directory like this:
    Access images in the `app/assets/images` directory like this:
    ```html
    <%= image_tag "rails.png" %>
  13. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -131,6 +131,8 @@ class CreatePhotos < ActiveRecord::Migration
    end
    ```

    Reference: http://guides.rubyonrails.org/active_model_basics.html

    ### Migrations

    Migration Data Types
  14. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -98,6 +98,8 @@ Generate a new controller with default actions, routes and views
    $ rails g controller Photos index show
    ```

    Reference: http://guides.rubyonrails.org/action_controller_overview.html

    ### Models

    Generate a model and create a migration for the table
  15. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -80,6 +80,8 @@ Create a route to a static view, without an action in the controller
    get 'photos/about', to: 'photos#about'
    ```

    Reference: http://guides.rubyonrails.org/routing.html

    ### Controllers

    Generate a new controller
  16. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -222,6 +222,8 @@ $ rake db:migrate

    Rollback the last migration performed

    **NOTE:** Be VERY careful with this command in production, it's destructive and you could potentially lose data. Make sure you absolutely understand what will happen when you run it

    ```
    $ rake db:rollback
    ```
  17. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ Create resources for only certain actions
    # config/routes.rb
    resources :photos, :only => [:index]

    # On flip side, you can create a resource with exceptions
    # On the flip side, you can create a resource with exceptions
    resources :photos, :except => [:new, :create, :edit, :update, :show, :destroy]
    ```

  18. Mike Dang revised this gist Jan 14, 2016. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -51,15 +51,15 @@ Automagically create all the routes for a RESTful resource
    resources :photos
    ```

    HTTP Verb | Path | Controller#Action
    --------- | ---- | -----------------
    GET | /photos | photos#index
    GET | /photos_new | photos#new
    POST | /photos | photos#create
    GET | /photos/:id | photos#show
    GET | /photos/:id/edit | photos#edit
    PATCH/PUT | /photos/:id | photos#update
    DELETE | /photos/:id | photos#destroy
    HTTP Verb | Path | Controller#Action | Used for
    --------- | ---- | ----------------- | -------
    GET | /photos | photos#index | display a list of all photos
    GET | /photos_new | photos#new | return an HTML form for creating a new photo
    POST | /photos | photos#create | create a new photo
    GET | /photos/:id | photos#show | display a specific photo
    GET | /photos/:id/edit | photos#edit | return an HTML form for editing a photo
    PATCH/PUT | /photos/:id | photos#update | update a specific photo
    DELETE | /photos/:id | photos#destroy | delete a specific photo

    Create resources for only certain actions

  19. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,7 @@ get 'photos/show', :to 'photos#show'
    Automagically create all the routes for a RESTful resource

    ```rb
    # config/routes.rb
    resources :photos
    ```

    @@ -60,6 +61,16 @@ GET | /photos/:id/edit | photos#edit
    PATCH/PUT | /photos/:id | photos#update
    DELETE | /photos/:id | photos#destroy

    Create resources for only certain actions

    ```rb
    # config/routes.rb
    resources :photos, :only => [:index]

    # On flip side, you can create a resource with exceptions
    resources :photos, :except => [:new, :create, :edit, :update, :show, :destroy]
    ```

    Create a route to a static view, without an action in the controller

    ```rb
  20. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -66,7 +66,7 @@ Create a route to a static view, without an action in the controller
    # config/routes.rb
    # If there's a file called 'about.html.erb' in 'app/views/photos', this file will be
    # automatically rendered when you call localhost:3000/photos/about
    get "photos/about", to: 'photos#about'
    get 'photos/about', to: 'photos#about'
    ```

    ### Controllers
  21. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -185,7 +185,7 @@ end
    ### Scaffolding

    ```
    $ rails g scaffold Photo title:text body:text image:text
    $ rails g scaffold Photo path:string caption:text
    $ rake db:migrate
    ```

  22. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -272,6 +272,8 @@ rel="stylesheet" />
    <script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
    ```

    Reference: [http://guides.rubyonrails.org/asset_pipeline.html](http://guides.rubyonrails.org/asset_pipeline.html)

    ### Form Helpers

    Create a basic form that posts to itself
    @@ -328,4 +330,6 @@ end
    <textarea id="photos_caption" name="photo[caption]" cols="60" rows="12"></textarea>
    <input name="commit" type="submit" value="Create" />
    </form>
    ```
    ```

    Reference: [http://guides.rubyonrails.org/form_helpers.html](http://guides.rubyonrails.org/form_helpers.html)
  23. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -324,8 +324,8 @@ end
    ```
    ```html
    <form accept-charset="UTF-8" action="/photos/create" method="post" class="nifty_form">
    <input id="photos_title" name="photo[path]" type="text" />
    <textarea id="photos_body" name="photo[caption]" cols="60" rows="12"></textarea>
    <input id="photos_path" name="photo[path]" type="text" />
    <textarea id="photos_caption" name="photo[caption]" cols="60" rows="12"></textarea>
    <input name="commit" type="submit" value="Create" />
    </form>
    ```
  24. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -305,4 +305,27 @@ Create a form with a custom action and method
    <input id="q" name="q" type="text" />
    <input name="commit" type="submit" value="Search" />
    </form>
    ```

    Bind a form to a model

    ```rb
    # app/controllers/photos_controller.rb
    def new
    @photo = Photo.new
    end
    ```
    ```rb
    <%= form_for @photo, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
    <%= f.text_field :path %>
    <%= f.text_area :caption, size: "60x12" %>
    <%= f.submit "Create" %>
    <% end %>
    ```
    ```html
    <form accept-charset="UTF-8" action="/photos/create" method="post" class="nifty_form">
    <input id="photos_title" name="photo[path]" type="text" />
    <textarea id="photos_body" name="photo[caption]" cols="60" rows="12"></textarea>
    <input name="commit" type="submit" value="Create" />
    </form>
    ```
  25. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -287,4 +287,22 @@ Create a basic form that posts to itself
    <input name="authenticity_token" type="hidden" value="J7CBxfHalt49OSHp27hblqK20c9PgwJ108nDHX/8Cts=" />
    Form contents
    </form>
    ```

    Create a form with a custom action and method

    ```rb
    <%= form_tag("/search", method: "get") do %>
    <%= label_tag(:q, "Search for:") %>
    <%= text_field_tag(:q) %>
    <%= submit_tag("Search") %>
    <% end %>
    ```
    ```html
    <form accept-charset="UTF-8" action="/search" method="get">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <label for="q">Search for:</label>
    <input id="q" name="q" type="text" />
    <input name="commit" type="submit" value="Search" />
    </form>
    ```
  26. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -149,10 +149,27 @@ class AddDateTakenToPhotos < ActiveRecord::Migration[5.0]
    end
    ```

    You can also add a new column to a table with an index

    ```
    $ rails g migration AddDateTakenToPhotos date_taken:datetime:index
    ```

    The above command generates the following migration:

    ```rb
    class AddDateTakenToPhotos < ActiveRecord::Migration[5.0]
    def change
    add_column :photos, :date_taken, :datetime
    add_index :photos, :date_taken
    end
    end
    ```

    The opposite goes for migration names following the format: `RemoveXXXFromYYY`

    ```
    $ rails g migration RemoveDateTakenFromPhotos part_number:datetime
    $ rails g migration RemoveDateTakenFromPhotos date_taken:datetime
    ```

    The above generates the following migration:
  27. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -149,6 +149,22 @@ class AddDateTakenToPhotos < ActiveRecord::Migration[5.0]
    end
    ```

    The opposite goes for migration names following the format: `RemoveXXXFromYYY`

    ```
    $ rails g migration RemoveDateTakenFromPhotos part_number:datetime
    ```

    The above generates the following migration:

    ```rb
    class RemoveDateTakenFromPhotos < ActiveRecord::Migration[5.0]
    def change
    remove_column :photos, :date_taken, :datetime
    end
    end
    ```

    ### Scaffolding

    ```
  28. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -133,6 +133,22 @@ Migration Data Types
    * `:time`
    * `:timestamp`

    When the name of the migration follows the format `AddXXXToYYY` followed by a list of columns, it will add those columns to the existing table

    ```
    $ rails g migration AddDateTakenToPhotos date_taken:datetime
    ```

    The above creates the following migration:

    ```rb
    class AddDateTakenToPhotos < ActiveRecord::Migration[5.0]
    def change
    add_column :photos, :date_taken, :datetime
    end
    end
    ```

    ### Scaffolding

    ```
  29. Mike Dang revised this gist Jan 13, 2016. No changes.
  30. Mike Dang revised this gist Jan 13, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions RAILS_CHEATSHEET.md
    Original file line number Diff line number Diff line change
    @@ -160,6 +160,12 @@ Run any pending migrations
    $ rake db:migrate
    ```

    Rollback the last migration performed

    ```
    $ rake db:rollback
    ```

    ### Path Helpers

    Creating a path helper for a route