LoginSignup
1
3

More than 5 years have passed since last update.

Rails メモ

Last updated at Posted at 2016-12-16

はじめに

Qiitaの下書きがいっぱいになった&公開したら良いじゃんというのをうけ公開する。
色々書いてるからRails以外のことにも結構とんでる

メモなので新しいことがあると更新していく

思想

  • CoC (Convention over Configuration)

設定より規約
慣例に従う範囲では設定不要
例:ファイルの自動読み込み, routingのresourceでアクションやパスが決まる, など

  • DRY (Don't Repeat Yourself)

重複を防ぐ→ 変更容易性が高くなる
例:concerns, partial view(部分テンプレート)

  • REST (REpresentational State Transfer)

一意なURLでコンテンツが認識できる

image

フォルダ構成

手順

$ rails new アプリ名
$ rails server -b 0.0.0.0

generate系でよく使うの

DB

config/datebase.yml に使用するDB情報が記述されている
db/scheme.rb は現在のRailsが管理しているDB情報を持っている
rails db でcl上でDBの情報を確認できる

モデルファイルを作成

$ rails generate model user name:string username:string location:string about:text

この2つができる
app/models/user.rb
db/migrate/~.rb

DBの作成・削除

$ rake db:create
$ rake db:drop

tableの作成

$ rake db:migrate

seedを追加

$ rake db:seed

データベースを実際に作成

$ rake db:migrate

シードの追加
db/seeds.rb に下のように追加

@user = User.new
@user.name = 'Ryo Suzuki'
@user.username = 'ryooopan'
@user.location = 'Kanagawa, Japan'
@user.about = 'Hello, I am Ryo. I am from database!'
@user.save

or

Title.create(:name => '観察日記', :sales_date => '2011-11-14', :price => 1000)

こんな感じで追加して

$ rake db:seed

model操作のクエリ 色々
http://qiita.com/merrill/items/8ec158953cb4c2715c7b

migration デフォルト

changeのメソッドが入っているが、changeを適用できるメソッドとそうじゃないものがある。
そのためchangeを適用できないものは、up/downを自分で書く

追加したカラムを同じマイグレーション内で扱う

追加後に以下を挟む

モデル名.reset_column_information

sqlite dump

$ rails db のちdump
dump
http://www.dbonline.jp/sqlite/manage/index4.html
import
http://www.dbonline.jp/sqlite/manage/index5.html

new

ページはnewのみ
newはcontrollerでモデルを作成、viewはモデルに合わせたフォームを作成
post先はroutingで勝手にcreateに
createは、controllerのみでparamsで受け取ったデータでモデルを作成、その後saveしrender indexが一般的

モデルはnewとbuildがある。
どちらかというとbuildがprefer
http://qiita.com/sukechansan/items/6bae532b4f678fdcf87d

ページの追加

rails g controller (モデル名) ページ名

app/controllers/ページ名 にコントローラ(ページ間の繋がり)を記述

config/routes.rb にget postの関係を記述

パーシャル(partial)

renderで外部ファイルを呼ぶ
ページ分割などに使う

アセットパイプライン

マニフェストファイル,プリプロセッサエンジンなども
https://railstutorial.jp/chapters/filling_in_the_layout?version=5.1#sec-the_asset_pipeline

ルーティング

railsのルーティングの一般的なものは、config/routes.rb に

resources :モデル名s

rake routesで確認可能(Webページで適当なURL打ってもOK)

rails runner

railsの環境を読み込んだ上で任意のrubyコードが実行できる
http://qiita.com/3yatsu/items/416411c0a8f696dbf99e

自作クラス

lib下に置く
リンクに複数パターン記述されている。
http://qiita.com/azusanakano/items/885fe3236977580b00c9

rakeタスクの作成

rails g task タスク名で作成可能
http://morizyun.github.io/blog/rake-task-rails-rspec-test/

デバック

rubyのpryをrailsの環境を読み込んだうえで実行してくれる。

$rails console

binding.pryとコードに打ち込むと、そこがブレイクポイントになる。

bootstrap

lessとsassのものがあるのを確認
sassのほうが導入が楽だった
http://qiita.com/shizuma/items/83cdadbe0a629f1f74d1

less

http://qiita.com/kamizuno/items/5c3ecceea5aa78e25058
https://github.com/seyhunak/twitter-bootstrap-rails

sass

http://techacademy.jp/magazine/7663
https://github.com/twbs/bootstrap-sass

bootstrap switch

optionは公式に記述
https://github.com/manuelvanrijn/bootstrap-switch-rails

bootstrapのデフォルト色以外を使う場合
http://stackoverflow.com/questions/29575437/how-to-customize-bootstrap-switch-color

こんなのもある
http://www.w3schools.com/bootstrap/bootstrap_button_groups.asp

ほのか

日本語をきれいに表示できる拡張フレームワーク
http://qiita.com/iguchi1124/items/7a2fe94d0897e217290d

mdl

material design liteを使う
http://qiita.com/rsakao/items/d0e6e2aa3f5180d5c2de

turbolinksを消しても良いが、遅くなるのでこちらで対応
app/assets/javascripts/application.jsに以下を追加

document.addEventListener('turbolinks:load', function() {
  componentHandler.upgradeDom();
});

document.addEventListener('page:change', function() {
  componentHandler.upgradeDom();
});

クエリ

一般的なもの
http://qiita.com/merrill/items/8ec158953cb4c2715c7b
日付や範囲
http://qiita.com/ishidamakot/items/4ca054a2204c174003b9

serializers

attributes でJSONで返すべき値を決める

Sprockets

sassやcoffeescriptなどを自動でコンパイルしてくれる

slimを使う

reactをどう使うか

react_webpack_rails
https://github.com/netguru/react_webpack_rails

共通部分を切り出す

Helper
viewで同じ要素だが一部だけ要素が違う場合などに使用
http://qiita.com/shunsuke227ono/items/21f5968ca7ca0391b583

scope

modelに記述, クエリを簡単にかける&使える
また、scopeをつなげることでAND, ORを再現できる
http://ruby-rails.hatenadiary.com/entry/20140814/1407994568

renderメソッド 色々

多対多

中間テーブルについての説明
http://gomocool.net/gomokulog/?p=820
railsでの多対多
https://railsguides.jp/association_basics.html#has-many-through%E9%96%A2%E9%80%A3%E4%BB%98%E3%81%91
https://qiita.com/Kohei_Kishimoto0214/items/cb9a3d3da57708fb52c9

secrets周り

ver5.1 以上
https://qiita.com/kikunantoka/items/dd22cf51071b93ba5b62
https://qiita.com/yoan/items/1a98371fe7ac524379e9

form_for

エラー後、同じページにrenderされた場合、以下のクラスが付与される
このクラスを使って、エラーのフォームを赤にしたりが簡単にできる
class="field_with_errors"

renderとredirect_toの違い

renderは変数などを保ったままの遷移、redirect_toは新規

https://qiita.com/1ulce/items/282cccba1e44158489c8
https://qiita.com/ko8@github/items/c0e7d2511b569e2d892e

flash

ページ遷移中のメッセージの表示などに便利
http://railsdoc.com/references/flash
renderとredirect_toでの使い分け
https://qiita.com/seimiyajun/items/6a3e0236b40016f37b52

名前の変換

railsでどう変換されているのか見たいときに
https://qiita.com/hmuronaka/items/79208f54bb88397517a4

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3