My Rails note
Tags: rails
Published: 2019-09-11 06:49:22

---
title: My Rails note
description: This is my main rails note.
tags: rails
series: note
published: true
cover_image:
canonical_url:
---

## Tips
### Rails tips
- 📚 [Ruby on Rails API](https://api.rubyonrails.org/)
- 📚 [Rails Guide](https://guides.rubyonrails.org)
  - 📚 [Rails Guid Edge](https://edgeguides.rubyonrails.org/)
  - 📚 [Rails Guid Japanese edition](https://railsguides.jp/)
- 💡 force stop rails server
  - `kill -9 $(cat tmp/pids/server.pid)`
{% link n350071/path-resolve-1dc %}


### Ruby tips
{% link n350071/ruby-awesome-methods-19p6 %}
{% link n350071/switch-a-couple-of-variable-without-tmp-in-ruby-20ja %}
{% link n350071/stop-checking-if-it-s-nil-in-rails-17gi %}
{% link n350071/start-eachwithindex-from-1-this-is-good-for-ui-2hj1 %}
{% link n350071/convert-to-hash-from-a-couple-of-the-key-value-array-in-rails-2ag6 %}
{% link n350071/the-ary-map-tos-what-is-this-n2i %}
{% link n350071/definemethod-write-once-run-everywhere-1d0i %}
{% link n350071/refactor-do-end-block-to-for-reduce-scrolling-in-rails-38bd %}
{% link n350071/ruby-array-multiple-assignment-34df %}
{% link n350071/ruby-heredocument-1mkg %}

## Design/Architecter Pattern
{% link n350071/concern-split-and-spin-off-share-the-responsibility-in-rails-11ol %}
{% link n350071/form-object-pattern-in-rails-19la %}
{% link n350071/object-hash-pattern-in-ruby-4j1h %}
{% link n350071/service-class-by-poro-10m0 %}
- decorator
  - [draper](https://github.com/drapergem/draper)

## Gems
{% link n350071/overwrite-a-gem-method-by-inserting-a-module-in-your-inheritance-chain-44g5 %}

### Authorization
{% link n350071/organize-the-authorizations-of-the-banken-pundit-gem-with-definemethod-2gei %}
{% link n350071/devise-arbitrary-routing-3l7h %}

### State machines, Status
- aasm

### Code Quarity
{% link n350071/rubocop-for-code-quality-and-formatting-in-rails-dn %}
{% link n350071/static-security-analysis-for-rails-app-by-using-breakman-283a %}
{% link n350071/metrics-your-test-code-coverage-in-rails-ala %}

## Rails Standard Features
- secrets, credentials

### Model
{% link n350071/model-table-name-conversions-in-rails-56hj %}
{% link n350071/a-common-validations-concern-in-rails-1e33 %}
{% link n350071/constant-enum-in-activerecord-rails-4gpe %}
{% link n350071/making-a-nested-params-for-xxxtag-of-view-helper-in-rails-377i %}
{% link n350071/how-to-write-save-and-save-bang-in-rails-29lk %}

### View & Helper

{% link n350071/view-helper-tips-note-1jnb %}
{% link n350071/pass-data-from-rails-to-js-2745 %}

#### Form
{% link n350071/form-tags-of-the-actionview-helpers-in-rails-4e0d %}
{% link n350071/load-stylesheet-from-controllerpath-o98 %}
{% link n350071/specify-layout-file-in-rails-views-2nfb %}

### Controller
#### where the request comes from?
```ruby
# confirm the request comes from `new_book_path`
request.referer.to_s.index(new_book_path)
```

#### Ping controller for the Server Monitoring(死活監視)
```ruby
class PingController < ApplicationController
  def index
    render text: 'OK', layout false
  end
end
```

#### iframe Same Origin Policy
```ruby
class HogeController < ApplicationController
  after_action :allow_iframe_for_trusted_domain, only[:index]

  private
  # Turn enable to see a page in a specific domain by using this method.
  # Because of the Same-Origin-Policy, it can't do it by default.
  def allow_iframe_for_trusted_domain
    alllow_url = "https://n350071.com"
    response.headers['X-Frame-Options'] = "ALLOW-FROM #{alllow_url}"
    response.headers['Content-Security-Policy'] = "frame-ancestors #{alllow_url}" # Chrome用
  end  
end
```


### Routes
#### pass a model with some params
```ruby
# The request is `article/:id?author=n350071`
article_path(@article, author: 'n350071')
```

### I18n
{% link n350071/i18n-in-rails-1in9 %}

### DB
{% link n350071/sql-tuning-in-rails-12fa %}

### rake
{% link n350071/rake-task-memo-n8m %}

## Meta Programing
{% link n350071/append-a-method-to-all-attributes-with-attributemethodsuffix-in-rails-2ngb %}
{% link n350071/ruby-instant-performance-measurement-2kcp %}
{% link n350071/mattraccessor-and-meta-programming-thanks-you-deviseinvitable-1hi2 %}
{% link n350071/the-eval-meta-programming-power-making-env-file-and-set-constant-values-from-the-file-3b43 %}

## Debug
### Dive into Gem file
#### 1. set your favorit editor
```sh
# bash_profile
export EDITOR="/Applications/Atom.app/Contents/MacOS/Atom"
```

Don't forget to `. ~/.bash_profile` after updating.

#### 2. open the bundled gem
just do it like this.
```sh
bundle open faker
bundle open banken
bundle open aasm
```



## Setting / Config
- [rails-settings-cached](https://github.com/huacnlee/rails-settings-cached)
{% link n350071/return-a-static-error-page-by-rails-standard-function-574a %}

{% github n350071/rails-starter-kit-with-docker %}