2016-03-31 4 views
0

私は、再生された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 

:問題が原因で発生し

答えて

1

elsif @game.home_score < @game.away_score 

それからこれは、メソッドの始まりの問題を引き起こしていた最後の2 end

の1を削除することができます/終わりと条件付きの始まり/終わり。

+0

私は愚かな過ちを嫌いです。どうもありがとう! –

+0

問題ありません!我々はすべてそこにいた。 – MTarantini

0

MVCのモデルは、ビジネスロジックの実施を担当します。したがって、コントローラではなく、この計算がモデルで実行されるはずです。

class Game < ActiveRecord::Base 

    # ... 

    belongs_to :winner, class_name: 'User' # or Team or whatever 
    belongs_to :loser, class_name: 'User' 

    before_validation :evaluate_score!, if: -> { home_score.present? } 

    private 

    def evaluate_score! 
     self.winner = home_score > away_score ? home_team_user : away_team_user 
     self.loser = home_score < away_score ? home_team_user : away_team_user 
    end 
end 

さらに、このラインは非効率的である。

@user_options = User.all.map{|u| [ u.user_name, u.id ] } 

それはDBから全レコードをプルし、メモリにそれらをループするので。

<%= f.association :away_team_user, collection: User.all, label_method: :user_name %> 

    <%= f.collection_select(:away_team_user, User.all, :id, :user_name) %> 
    

    単純な形式のRails collection_*ヘルパーをアップ朝飯前associationヘルパーメソッドを持っている:あなたは@user_options = User.pluck(:username, :id)が、レールは、コレクションからの入力を作成するための偉大なヘルパーを持っているので、それは必要ありませんが、使用することができます

  • https://github.com/plataformatec/simple_form
+0

私は同意します。ニース追加! – MTarantini

+0

ありがとう、私はそれをチェックします! –

関連する問題