-
-
Save chilianubogdan/a7fa15dbc40a5f49f5150fab9c91d59c to your computer and use it in GitHub Desktop.
Revisions
-
Mike Dang revised this gist
Mar 9, 2016 . No changes.There are no files selected for viewing
-
Mike Dang revised this gist
Mar 9, 2016 . 1 changed file with 20 additions and 33 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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="✓" /> <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) -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ ## Ruby on Rails Cheatsheet <img src="https://raw.githubusercontent.com/mdang/resources/master/ruby/rails/rails_architecture.png"> ### Create a new application -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ ### Architecture <img src="https://raw.githubusercontent.com/mdang/resources/master/ruby/rails/rails_architecture.png"> ### Create a new application -
Mike Dang revised this gist
Mar 7, 2016 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -337,7 +337,7 @@ 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" %> -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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> -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 `app/assets/images` directory like this: ```html <%= image_tag "rails.png" %> -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 the flip side, you can create a resource with exceptions resources :photos, :except => [:new, :create, :edit, :update, :show, :destroy] ``` -
Mike Dang revised this gist
Jan 14, 2016 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 | 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 -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' ``` ### Controllers -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -185,7 +185,7 @@ end ### Scaffolding ``` $ rails g scaffold Photo path:string caption:text $ rake db:migrate ``` -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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_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> ``` -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> ``` -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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="✓" /> <label for="q">Search for:</label> <input id="q" name="q" type="text" /> <input name="commit" type="submit" value="Search" /> </form> ``` -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 18 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 date_taken:datetime ``` The above generates the following migration: -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
Mike Dang revised this gist
Jan 13, 2016 . No changes.There are no files selected for viewing
-
Mike Dang revised this gist
Jan 13, 2016 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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
NewerOlder