Rails - 嵌入if语句,意外结果

时间:2015-06-30 21:51:45

标签: ruby-on-rails ruby

我的文章控制器中的if语句出现意外结果。我的目的是允许文章作者或管理员用户(它是users表中的布尔列)更新文章。我想检查当前用户是否是原始作者,如果是,请更新作者姓名,以防该人在发布文章后更改了用户名。

问题是当管理员更新文章时,它不仅将作者名称更改为admin,还将user_id设置为admin!我不明白为什么。 admin.id与article.user_id不匹配,为什么会发生这种情况?

这是文章控制器中的一点:

def update
@article = Article.find(params[:id])

if @article.user_id == current_user.id || current_user.admin?
  #update author in case of user name change
    if @article.user_id = current_user.id
      @article.author = current_user.name
    end 
  if @article.update(article_params)
        redirect_to @article
else
    render 'edit'
end

elsif
  redirect_to articles_path, :notice => "You do not have permission to edit this resource."
end

end

1 个答案:

答案 0 :(得分:0)

if @article.user_id = current_user.id

这是一项任务(并且始终为真)

需要if @article.user_id == current_user.id

相关问题