class BoardPostsController < ApplicationController before_action :require_login before_action :set_game before_action :set_board before_action :set_current_participant def create # 参加チェック unless @board.member?(@current_participant) && @game.status == "in_progress" return redirect_to game_board_path(@game, @board), alert: "投稿権限がありません" end @post = @board.board_posts.new(post_params) @post.participant = @current_participant # フェーズ情報の付与 # 最新のターン情報を取得してセットする latest_turn = @game.turns.order(number: :desc).first if latest_turn @post.phase = latest_turn.phase end if @post.save redirect_to game_board_path(@game, @board) # noticeはチャット的にうるさいので省略 else redirect_to game_board_path(@game, @board), alert: "投稿に失敗しました" end end private def set_game @game = Game.find(params[:game_id]) end def set_board @board = @game.boards.find(params[:board_id]) end def set_current_participant @current_participant = @game.participants.find_by(user: current_user) end def post_params params.require(:board_post).permit(:body) end end