【发布时间】:2018-04-12 15:41:10
【问题描述】:
我有一个控制器方法如下
def create
@game = Game.create_new_game(game_params)
render 'show', status: 200
rescue StandardError => e
render json: {
status: 500,
error: e.to_s
}
end
我添加了binding.pry,我可以在控制台中清楚地看到以下错误:
#<ActiveRecord::RecordInvalid: Validation failed: Name can't be blank, Duration can't be blank>
但它仍然将status:200 发送到客户端。是否应该以不同的方式处理错误?
编辑:
Game 中的create_new_game 方法
def self.create_new_game(prms)
@player = Player.find(prms[:player].to_i)
@game = @player.games.create!(
name: prms[:name],
duration: prms[:duration]
)
@game.save!
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 error-handling standard-error