└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # useful_stuff 2 | Memo for my beloved students 3 | 4 | ## Summary 5 | 6 | - [Keyboard's shortcuts (Mac)](#keyboards-shortcuts-mac) 7 | - [Special characters](#special-characters) 8 | - [Navigate](#navigate) 9 | - [Text Editor](#text-editor) 10 | - [Terminal](#terminal) 11 | - [Chrome](#chrome) 12 | - [Keyboard's shortcuts (LINUX)](#keyboards-shortcuts-linux) 13 | - [Text Editor](#text-editor-1) 14 | - [Terminal](#terminal-1) 15 | - [Frequently misunderstood error messages](#frequently-misunderstood-error-messages) 16 | - [NoMethodError: undefined method `some_method' for some_object:SomeType](#nomethoderror-undefined-method-some_method-for-some_objectsometype) 17 | - [TypeError: SomeType can't be coerced into SomeOtherType](#typeerror-sometype-cant-be-coerced-into-someothertype) 18 | - [TypeError: no implicit conversion of SomeType into SomeOtherType](#typeerror-no-implicit-conversion-of-sometype-into-someothertype) 19 | - [ArgumentError: wrong number of arguments (given N, expected M)](#argumenterror-wrong-number-of-arguments-given-n-expected-m) 20 | - [PG::Error FATAL "myapp_development" does not exist](#pgerror-fatal-myapp_development-does-not-exist) 21 | - [ActiveRecord::PendingMigrationError Migrations are pending](#activerecordpendingmigrationerror-migrations-are-pending) 22 | - [PG::ConnectionBad - could not connect to server: No such file or directory](#pgconnectionbad---could-not-connect-to-server-no-such-file-or-directory-is-the-server-running-locally-and-accepting-connections-on-unix-domain-socket-tmpspgsql5432) 23 | - [NameError: uninitialized constant ClassName](#nameerror-uninitialized-constant-classname) 24 | - [ResourcesController#action is missing a template for this request format and variant.](#resourcescontrolleraction-is-missing-a-template-for-this-request-format-and-variant) 25 | - [Pundit::NotDefinedError in SomeController#some_action](#punditnotdefinederror-in-somecontrollersome_action) 26 | - [Pundit::AuthorizationNotPerformedError in SomeController#some_action](#punditauthorizationnotperformederror-in-somecontrollersome_action) 27 | - [Pundit::NotAuthorizedError in SomeController#some_action](#punditnotauthorizedderror-in-somecontrollersome_action) 28 | - [Mastering git](#mastering-git) 29 | - [Starting a new feature](#starting-a-new-feature) 30 | - [Finishing a feature](#finishing-a-feature) 31 | - [Getting latest changes from master](#getting-latest-changes-from-master) 32 | - [Conflicts solving](#conflicts-solving) 33 | - [Accidental commit to master](#fix-an-accidental-commit-to-master) 34 | - [Useful gems](#useful-gems) 35 | - [Going further](#going-further) 36 | 37 | ## Keyboard's shortcuts (Mac) 38 | ### Special characters 39 | ```ruby 40 | { # alt + ( 41 | } # alt + ) 42 | [ # alt + shift + ( 43 | ] # alt + shift + ) 44 | | # alt + shift + l 45 | ~ # alt + n 46 | \ # alt + shift + / 47 | ``` 48 | 49 | ### Navigate 50 | ```ruby 51 | from one program to another # cmd + tab 52 | from one window to another (same program) # cmd + ` 53 | ``` 54 | 55 | ### Text Editor 56 | 57 | For Sublime Text or VS Code with Sublime Text Keymap 58 | 59 | ```ruby 60 | open a file in current project # cmd + p (or cmd + t) 61 | paste with indentation # cmd + shift + v 62 | move a line upwards in file # ctrl + cmd + ↑ 63 | move a line downwards in file # ctrl + cmd + ↓ 64 | (un)comment lines # cmd + / 65 | search in file # cmd + f 66 | search in project # cmd + shift + f 67 | add package # cmd + shift + p 68 | add indentation # tab 69 | remove indentation # shift + tab 70 | reach next word # alt + → 71 | reach previous word # alt + ← 72 | reach end of line # cmd + → 73 | reach beginning of line # cmd + ← 74 | close tab # cmd + w 75 | reopen last tab closed # cmd + shift + t 76 | navigate to tab on the right # cmd + alt + → 77 | navigate to tab on the left # cmd + alt + ← 78 | duplicate line # cmd + shift + d 79 | remove line # ctrl + shift + k 80 | split view into two columns # cmd + option + 2 81 | split view into two rows # cmd + option + shift + 2 82 | ``` 83 | 84 | ### Terminal 85 | ```ruby 86 | open a new tab # cmd + t 87 | close tab # cmd + w 88 | clear window (keeping history) # ctrl + l 89 | clear window (losing history) # cmd + k 90 | reach next word # alt + → 91 | reach previous word # alt + ← 92 | reach beginning of line # ctrl + a 93 | reach end of line # ctrl + e 94 | erase the whole line # ctrl + u 95 | navigate to tab on the right # cmd + shift + → 96 | navigate to tab on the left # cmd + shift + ← 97 | ``` 98 | 99 | ### Chrome 100 | ```ruby 101 | open developer tools (elements) # cmd + alt + i 102 | open developer tools (console) # cmd + alt + j 103 | change docking location # cmd + shift + d 104 | 105 | navigate to tab on the right # cmd + alt + → 106 | navigate to tab on the left # cmd + alt + ← 107 | open a new tab # cmd + t 108 | focus on address bar # cmd + l 109 | close tab # cmd + w 110 | reopen last tab closed # cmd + shift + t 111 | 112 | hard refresh (clear cache) # cmd + shift + r 113 | ``` 114 | 115 | ## Keyboard's shortcuts (LINUX) 116 | 117 | ### Text Editor 118 | 119 | For Sublime Text or VS Code with Sublime Text Keymap 120 | 121 | ```ruby 122 | open a file in current project # ctrl + p 123 | paste with indentation # ctrl + shift + v 124 | move a line upwards in file # ctrl + shift + ↑ 125 | move a line downwards in file # ctrl + shift + ↓ 126 | (un)comment lines # ctrl + shift + : 127 | search in file # ctrl + f 128 | search in project # ctrl + shift + f 129 | add package # ctrl + shift + p 130 | add indentation # tab 131 | remove indentation # shift + tab 132 | move text cursor to next word # alt + → 133 | move text cursor to previous word # alt + ← 134 | close tab # ctrl + w 135 | reopen last tab closed # ctrl + shift + t 136 | duplicate line # ctrl + shift + d 137 | ``` 138 | 139 | ### Terminal 140 | ```ruby 141 | open a new tab # ctrl + shift + t 142 | close tab # ctrl + shift + w 143 | clear window # ctrl + L (or type "clear" in terminal) 144 | ``` 145 | 146 | ## Common error messages 147 | The first advice I'll throw here is to **read patiently, entirely, twice** the error message when one occurs. 148 | This can get tricky when you read it from a tiny terminal window, so start by opening it wide to have a clear look at its face. 149 | 150 | ### Ruby 151 | 152 | #### ``NoMethodError: undefined method `some_method' for some_object:SomeType`` 153 | 154 | ```ruby 155 | # It means that `some_object` **on which you call** `.some_method` is not of the right type. 156 | # You'll get this error if you try to call a `String` instance method on a `Fixnum` for example. 157 | # Its most frequent expression is when `some_object` is nil: 158 | 159 | recipe.name 160 | # => NoMethodError: undefined method `name' for nil:NilClass 161 | # `recipe` is nil, it shouldn't! 162 | ``` 163 | 164 | #### `TypeError: SomeType can't be coerced into SomeOtherType` 165 | 166 | ```ruby 167 | # It means that you're trying to mix apples and oranges. 168 | 169 | 1 + nil 170 | # => TypeError: nil can't be coerced into Fixnum 171 | 172 | 2 + "2" 173 | # => TypeError: String can't be coerced into Fixnum 174 | ``` 175 | 176 | #### `TypeError: no implicit conversion of SomeType into SomeOtherType` 177 | 178 | ```ruby 179 | # Its cousin. 180 | 181 | "2" + 2 182 | # => TypeError: no implicit conversion of Fixnum into String 183 | ``` 184 | 185 | #### `ArgumentError: wrong number of arguments (given N, expected M)` 186 | 187 | ```ruby 188 | # It means that you're calling a method that takes M parameters with N arguments. 189 | # Either your definition of that method is wrong, either you're calling it with the wrong number of arguments. 190 | 191 | def full_name(first_name, last_name) 192 | return first_name.capitalize + " " + last_name.capitalize 193 | end 194 | 195 | full_name("boris") 196 | # => ArgumentError: wrong number of arguments (given 1, expected 2) 197 | ``` 198 | 199 | ### Rails 200 | 201 | #### `PG::Error FATAL "myapp_development" does not exist` 202 | 203 | ```ruby 204 | # Just run `rails db:create` in your terminal. 205 | ``` 206 | 207 | #### `ActiveRecord::PendingMigrationError Migrations are pending` 208 | ```ruby 209 | # Just run `rails db:migrate` in your terminal. 210 | ``` 211 | 212 | #### `PG::ConnectionBad - could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?` 213 | 214 | ```bash 215 | # It means that PG did not exit gracefully last time your computer was shut off. 216 | # Run the following in your terminal: 217 | 218 | rm /usr/local/var/postgres/postmaster.pid 219 | ``` 220 | 221 | #### `NameError: uninitialized constant ClassName` 222 | 223 | ```ruby 224 | # In most cases, you'll get this error message when you're implementing a gem 225 | # and you forgot to restart your `rails server` after running `bundle install`. 226 | ``` 227 | 228 | #### `ResourcesController#action is missing a template for this request format and variant.` 229 | 230 | ```ruby 231 | # After executing the code in a controller's action, Rails conventionnally 232 | # renders the template named `action.html.erb` in `app/views/resources`. 233 | # It thus means you forgot to generate action's associated view, that you 234 | # misspelled its filename, or that you misplaced it. 235 | ``` 236 | 237 | #### `Pundit::NotDefinedError in SomeController#some_action` 238 | 239 | ```bash 240 | # The policy is not defined for this controller! 241 | rails g pundit:policy **name_of_the_resource_in_singular** 242 | ``` 243 | 244 | #### `Pundit::AuthorizationNotPerformedError in SomeController#some_action` 245 | 246 | ```ruby 247 | # You need to call `authorize(@some_resource)` in the action (in the controller) 248 | # Make sure you insert this line before saving the resource in your DB! 249 | ``` 250 | 251 | #### `Pundit::NotAuthorizedError in SomeController#some_action` 252 | 253 | ```ruby 254 | # The rule returns false, you should override ApplicationPolicy's rule by setting it in corresponding ResourcePolicy 255 | # Don't forget to rescue this error with a flash and a redirection at ApplicationController's level! 256 | ``` 257 | 258 | ## Mastering git 259 | 260 | ### Starting a new feature 261 | 262 | When you want to start a new feature. 263 | You have to create a new branch to work from: 264 | 265 | ``` 266 | (master) git status (CLEAN) 267 | (master) git checkout -b my-feature 268 | ``` 269 | 270 | ### Finishing a feature 271 | 272 | You've just finished you feature. 273 | Now it's time to make a last commit, create a pull request and go back to master branch: 274 | 275 | ``` 276 | (my-feature) git add . 277 | (my-feature) git commit -m 'XXXX' 278 | (my-feature) git push origin my-feature 279 | 280 | (my-feature) git status (CLEAN) 281 | 282 | (my-feature) gh repo view --web 283 | [GO CREATE A PULL REQUEST ON GITHUB] 284 | 285 | (my-feature) git checkout master 286 | (master) git pull origin master 287 | (master) git sweep 288 | 289 | (master) git checkout -b my-new-feature 290 | ``` 291 | 292 | ### Getting latest changes from master 293 | 294 | When you want to get the latest changes from master on my branch: 295 | 296 | ``` 297 | (my-feature) git add . 298 | (my-feature) git commit -m 'XXXX' 299 | 300 | (my-feature) git status (CLEAN) 301 | 302 | (my-feature) git checkout master 303 | (master) git pull origin master 304 | 305 | (master) git checkout my-feature 306 | (my-feature) git merge master 307 | ``` 308 | 309 | ### Conflicts solving 310 | 311 | When you can't merge a PR due to conflicts in an `unmergeable_branch`, follow this process: 312 | 313 | ```ruby 314 | # first make sure your git status is clean on your current branch 315 | git status # if not clean, add and commit your work 316 | 317 | # go to your local master branch 318 | git checkout master 319 | 320 | # update your master branch locally with github's master 321 | git pull origin master 322 | 323 | # go back to unmergeable_branch 324 | git checkout unmergeable_branch 325 | 326 | # merger master in unmergeable branch to solve conflicts locally 327 | git merge master 328 | 329 | # now you've fetched all conflicts locally, time to solve them 330 | # open sublime and solve conflicts (locate them with cmd + shift + f <<<<<<<) 331 | 332 | # commit your code 333 | git add . 334 | git commit -m "conflict solving" 335 | git push origin unmergeable_branch 336 | 337 | # open your PR on github to merge it 338 | hub browse 339 | 340 | # merge your PR on github 341 | 342 | # go back to your terminal and update your local master with merged files 343 | git checkout master 344 | git pull origin master 345 | 346 | # create a new branch for next feature 347 | git checkout -b next_feature 348 | ``` 349 | 350 | ### Fix an accidental commit to master 351 | 352 | When you committed some changes to `master` and wanted to commit them to a new branch, follow this process: 353 | 354 | ```ruby 355 | # create a new branch from the current state of master 356 | git checkout -b some-new-branch-name 357 | 358 | # remove the commit from the master branch 359 | git checkout master 360 | git reset HEAD~ --hard 361 | 362 | # go back to your new branch 363 | git checkout some-new-branch-name 364 | # your commit lives in this branch now 365 | 366 | # if you want to make sure it does... 367 | git push origin some-new-branch-name 368 | 369 | # ...go check it out by yourself 370 | hub browse 371 | ``` 372 | 373 | ## Useful gems 374 | 375 | #### [Acts as votable](https://github.com/ryanto/acts_as_votable) 376 | Perfect for like / upvote / save to wishlist features! 377 | #### [Public Activity](https://github.com/chaps-io/public_activity) 378 | A good framework to handle notifications. 379 | Check [this railscast](http://railscasts.com/episodes/406-public-activity) out to set it up in your project! 380 | 381 | ## Going further 382 | 383 | #### [10 common mistakes using Rails](https://www.toptal.com/ruby-on-rails/top-10-mistakes-that-rails-programmers-make) 384 | #### [The Vital Guide to Ruby on Rails Interviewing](https://www.toptal.com/ruby-on-rails#skill_article_content_title) 385 | --------------------------------------------------------------------------------