プロダクト開発日誌

技術のことなど

Ruby on Rails

【Rails】1対多のリレーションを作成

Zoneモデルを作成し、AreaモデルとZoneモデルを1対多のリレーションを作成します。 Zoneモデルの作成 bin/rails g scaffold Zone name:string description:text Migration File class CreateZones < ActiveRecord::Migration[6.0] def change create_table :…

画像アップロード機能を追加(Active Storage)

ユーザモデルに画像を追加します。 bin/rails active_storage:install マイグレーションファイルを確認 class CreateActiveStorageTables < ActiveRecord::Migration[5.2] def change create_table :active_storage_blobs do |t| t.string :key, null: false…

Railsにユーザ認証機能を追加(devise)

install # Flexible authentication solution for Rails with Warden. gem 'devise' bundle install bin/rails g devise:install config/initializers/devise.eb ・・・ Deviseの設定を行うファイル config/locals/devise.en.yml ・・・ Deviseのローカライゼーシ…

RailsのER図を書き出すgem

voormedia.github.io rails-erdを利用するには Graphviz が必要です http://graphviz.org/ sudo port install graphviz Gem をインストール group :development do # Generate Entity-Relationship Diagrams for Rails applications. Read more: http://voor…

Git hooksの導入

github.com git hooksはコミットやマージの前にスクリプトを走らす機能 Hooksの運用に便利なgem overcommitをインストールする group :development do # A fully configurable and extendable Git hook manager # Read more: https://github.com/brigade/ove…

bundler の バージョンを指定する

bundler.io Install可能なバージョンを指定 gem search ^bundler$ --all バージョンを指定してインストール gem install bundler -v 2.0.1 インストール済みのバージョン gem list bundler バージョンを指定して実行 bundle _2.0.1_ install

Rails app に Rubocopを導入する

github.com Gemfile group :development do # A Ruby static code analyzer and formatter, based on the community Ruby style guide. https://docs.rubocop.org gem 'rubocop', require: false end bundle install touch .rubocop.yml AllCops: Rails: tru…

annotateでschemeをmodelに書き出す

github.com Gemfile group :development do gem 'annotate' end bundle install 使い方 bundle exec annotate テーブルの情報がコメントで記載されます # == Schema Information # # Table name: areas # # id :bigint(8) not null, primary key # name :str…

【Rails】モデルに都道府県情報を持たせる

今回はacrive__hashを利用して、Area Model に都道府県情報を付与します。 github.com install # A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource. Read more: https://github.com/zil…

デバック用のgemをrails アプリに追加する

Better Errors 標準のRailsエラーページを遥かに優れたエラーページに置き換えます。 github.com Binding of caller Better Errors のエラー画面でirbを利用できるようになる。 github.com Pry byebug 段階的なデバッグ機能とスタックナビゲーション機能を追…

rails scaffold コマンドでモデルを作成する

今回はエリアモデルを作成します。 bin/rails generate scaffold Area name:string description:string https://railsguides.jp/command_line.html#rails-generate nameは必須にします。 class CreateAreas < ActiveRecord::Migration[6.0] def change creat…

Rails アプリのテンプレートエンジンをSlimに変更する

github.com github.com Gemfile gem 'slim-rails' gem 'html2slim' gem をInstall bundle install --path vendor/bundle --jobs=4 erbをslimに変換 bundle exec erb2slim app/views/layouts/ --delete

最新のRuby on Rails でアプリを作成

rails のInstall bundle init rails のgemを追加 # Gemfile + gem 'rails', github: 'rails/rails' github.com rails をInstall bundle install --path vendor/bundle --jobs=4 rails のバージョンを確認 bundle exec rails -v Rails 6.0.0.beta3 新アプリの…

ざっくりHaml

Haml (HTML Abstraction Markup Lunguage) 概要 HTML template engin HTMLをスッキリ美しく 公式サイト: http://haml.info/ リファレンス: http://haml.info/docs/yardoc/file.REFERENCE.html コツ h1, tableなどは開始タグと閉じタグを削除し、%をタグにつ…

【Rails5】Herokuに乗っけたらフェイスブック認証ができなくなった件

エラー URLはブロックされています: リダイレクトURIがアプリのクライアントOAuth設定でホワイトリストに追加されていないため、リダイレクトできませんでした。クライアントとウェブOAuthログインをオンにして、すべてのアプリドメインを有効なOAuthリダイ…