current_user.id == @ game.user_id无法识别

时间:2012-03-09 17:36:26

标签: ruby-on-rails controller edit

我在games_controller编辑部分中有以下代码:

def edit
   @game = Game.find_by_game_name(params[:id])

  respond_to do |format|
    if current_user.id == @game.user_id
      format.html 
    else
      format.html { redirect_to games_path, notice: 'You are not authorized to change these details.'}
    end
  end
end

我的问题是,如果我点击编辑功能,即使是那些属于用户的游戏,我也会转到其他行,并注明我无权更改这些详细信息。游戏的user_id字段与用户的id匹配,但不允许我编辑。任何想法为什么会这样?

1 个答案:

答案 0 :(得分:0)

你几乎可以问同样的事情:

if current_user.id = @game.user_id

如果你这样做会有效。

在您当前的示例中,它可能无效,因为您未获得适当授权。尝试删除if,看看它是否完全没有它。