在控制器中使用if函数

时间:2016-03-31 15:17:25

标签: ruby-on-rails ruby if-statement

我试图制作一个存储国际足联比赛的应用程序。

现在我已经能够创建账号,存储游戏等。但是当我将游戏存储到数据库中时,我还希望以某种方式存储游戏的赢家和输家我可以稍后使用计数功能来计算用户有多少输赢。

控制器:

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&#39;对

     

正如您在控制器中看到的那样,game_params不会丢失。我在最后添加了end,因为这会导致加载表单时出错。

2 个答案:

答案 0 :(得分:1)

问题是由:

引起的
else if @game.home_score < @game.away_score

应该是:

elsif @game.home_score < @game.away_score

然后您可以删除最后两个end

中的一个

这导致方法开始/结束和条件开始/结束的问题。

答案 1 :(得分: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中拉出,然后在内存中循环遍历它们。你可以使用@user_options = User.pluck(:username, :id)但是没有必要,因为rails有很好的帮助来创建集合的输入:

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

Simple Form有一个association帮助器方法,它会使Rails collection_*帮助器受到阻碍:

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