パスワードリセット機能の実装

パスワードをリセットするためにモジュールをインストール rails g sorcery:install reset_password --only-submodules 作成されるマイグレーションファイル class SorceryResetPassword < ActiveRecord::Migration[5.2] def change add_column :users, :res…

プロフィール編集機能の実装

profiles_controllerの作成 rails g controller profiles profiles_controllerにアクションを追加する class ProfilesController < ApplicationController before_action :set_user, only: %i[edit update] def edit; end def update if @user.update(user_p…

掲示板の検索機能を実装

Gemfileに追記 gem 'ransack' bundle installする コントローラーへ検索機能を追加する def index @q = Board.ransack(params[:q]) @boards = @q.result(distinct: true).includes(:user).order(created_at: :desc) .page(params[:page]) end ビューに検索フ…

掲示板のページネーション

Gemfileに追記 gem 'kaminari' bundle installする コントローラーの変更 def index @boards = Board.all.includes(:user).order(created_at: :desc) def index @boards = Board.all.includes(:user).order(created_at: :desc).page(params[:page]) end kami…

ブックマーク機能の追加

Bookmarkモデルの作成 rails g model Bookmark user:references board:references class CreateBookmarks < ActiveRecord::Migration[5.2] def change create_table :bookmarks do |t| t.references :user, foreign_key: true t.references :board, foreign_…

掲示板の編集、削除機能の実装

boards_controllerに追加(edit update destroy set_board) class BoardsController < ApplicationController beforeaction :set_board, only: [:edit, :update, :destroy] def new @board = Board.new end def create @board = current_user.boards.build(bo…

タイトルを動的に出力する

helpers/application_helpers.rb module ApplicationHelper def page_title(page_title = '') base_title = "TestApp" page_title.empty? ? base_title : page_title + "|" + base_title end end page_title.empty? ? base_title : page_title + "|" + base_…

コメント機能の実装

commentモデルの作成 rails g model comment body:text user:references board:references アソシエーションの追加(user.rb, board.rb) has_many comments, dependent: :destroy ルーティングの定義 resources :boards do resources :comments, shallow: tru…

掲示板詳細画面の追加

以前作成した掲示板の機能に詳細画面を追加 boards_controller.rb def show @board = Board.find(params[:id]) end boards/show.html.erbの作成 <article class="card"> <div class="card-body"> <h3><%= @board.title %><h3> <%= @board.created_at %> </ul> </div> </div> <p><%= @board.body %></p> </div> </article> <%= link_to '掲示板一覧', boa…

掲示板の画像アップロード機能

Gemfileに追加 gem 'carrierwave' gem 'mini_magick' bundle installする アップローダの作成 rails g uploader Board_Image board_image_uploader.rbを作成 board_image_uploader.rb def default_url デフォルトの画像を指定 '.png' end def extension_whit…

フォーム入力時エラー情報を個別表示

掲示板作成の時に作ったform_withの中に <%= render 'shared/_error_messages', object: f.object %> を追加 _error_message.html.erb <% if object.errors.any? %> <div class="alert alert-danger> <ul class="mb-0"> <% object.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> for…

掲示板作成機能

まずboards_controllerにnewアクション、createアクション、privateメソッドでboard_paramsを作成 def new @board = Board.new end def create @board = cuurent_user.boards.build(boards_params) if @board.save redirect_to boards_path else render :new…

掲示板の一覧機能の実装

まずBoardモデルを作成(Userモデルと紐付けるため外部キーを保存できるカラムを設定する) rails g model Board title:string body:text user:references rails db:migrate これでuser_idを保存できるカラムをもったBoardテーブルができました。 アソシエーシ…

デコレーターの導入

Gemfileに gem 'draper' を追加してbundle installする。 次にrails g draper:installを実行する rails g draper:installを実行することによってrails g decorator モデル名が使えるようになる。 rails g decorator Userを実行することによってapp/decorator…

フラッシュメッセージの設定

まずコントローラーに表示したいメッセージを記述 例 flash[:notice] = "ログインしました" flash.now[:aleart] = "ログアウトしました" 次にviewにflashを表示させる <%= flash[:notice] %> これだけでflashメッセージは表示されます。 でも毎回<%= flash[:…

i18nによる日本語化対応

Gemfileに gem 'rails-i18n'を追加 bundle installする。 config/locales/application.rbに config.i18n.default_locale = :ja config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}') .to_s] を記載 config.i18n.default…

sorceryを使ってユーザー登録、ログイン機能の作成

Gemfileに gem 'sorcery'を追加 budnel installをする 次にrails g sorcery:installを行う。 user.rb usersテーブルを作成するためのマイグレーションファイルができるのでrake db:migrateでusersテーブルを作成する class SorceryCore < ActiveRecord::Migr…