私は、再生されたFIFAゲームを格納するアプリケーションを作成しようとしています。コントローラのif関数を使用する
私はすでに口座を開設したりゲームを保存していますが、DBにゲームを保存するときには、勝者と敗者をできる限り保存したいと思います後でカウント機能を使用して、ユーザーが獲得した勝敗の数をカウントします。
コントローラー:
class GamesController < ApplicationController
before_action :authenticate_user!, exept: [:index, :show]
def index
@games = Game.all
end
def new
@game = current_user.games.build
@user_options = User.all.map{|u| [ u.user_name, u.id ] }
end
def create
@user_options = User.all.map{|u| [ u.user_name, u.id ] }
@game = Game.new(game_params)
@game.home_team_user_id = current_user.id
if @game.home_score > @game.away_score
@game.winner_id = @game.home_team_user_id
@game.loser_id = @game.away_team_user_id
else if @game.home_score < @game.away_score
@game.winner_id = @game.away_team_user_id
@game.loser_id = @game.home_team_user_id
else
end
if @game.save
redirect_to games_path, :notice => "Successfully added game!"
else
render 'index'
end
end
def show
@games = Game.all
end
def destroy
@game = Game.find(params[:id])
@game.destroy
redirect_to games_path
end
private
def find_game
@game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:home_team_user_name, :home_score, :away_team_user_name, :away_score, :home_team_user_id, :away_team_user_id, :winner_id, :loser_id)
end
end
end
ビュー:
<div class="col-md-12" style="text-align:center">
<div class="panel panel-default" style="margin-right:10px">
<div class="panel-heading">
<h3 class="panel-title">Submit New Match</h3>
</div>
<div class="panel-body">
<%= simple_form_for(@game) do |f| %>
<%= f.text_field :home_score, :placeholder => "Your score" %>
<%= f.text_field :away_score, :placeholder => "Your Opponents score" %> <br><br>
<p>Opponent:</p>
<%= f.select(:away_team_user_id, @user_options) %>
<br> <br> <%= f.submit "Submit Match", class: "btn-submit" %>
<% end %>
</div>
</div>
これは、この計算を行うための正しい方法は何ですか?それとも、他の提案がありますか?
これは正しい方法であれば、私はフォームを送信しようとすると、なぜ私はこのエラーが出るん:
はgame_params未定義のローカル変数やメソッドが `
コントローラーに表示されているように、game_params
はありません。私はend
を最後に追加しましたが、フォームをロードする際にエラーが発生しました。それがあるべき
else if @game.home_score < @game.away_score
:問題が原因で発生し
私は愚かな過ちを嫌いです。どうもありがとう! –
問題ありません!我々はすべてそこにいた。 – MTarantini