【发布时间】:2021-04-29 07:05:46
【问题描述】:
以下是我为我的 Rails 显示审查应用程序编写的控制器之一的代码。请注意,我没有将 Devise 用于用户身份验证。
我现在面临的问题是我希望用户(pco)只有在他/她是最初上传节目的人时才能更新节目。在这里,authorized_as_pco_to_show 可以确定,但它需要将@show 作为参数传递给它。因此,我不能使用before_action。
我现在的方法是将这个authorized_as_pco_to_show 方法放在每个操作的开头,只允许正确的 pco 访问它。我想知道是否有更好的方法来做到这一点。任何帮助将不胜感激!
def update
authorized_as_pco_to_show @show
respond_to do |format|
if @show.update(show_params)
format.html { redirect_to @show, notice: "Show was successfully updated." }
format.json { render :show, status: :ok, location: @show }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @show.errors, status: :unprocessable_entity }
end
end
end
【问题讨论】:
-
@show来自哪里?如果在填充后将其插入,则可以在before_action中使用它。一切都是为了订购。
标签: ruby-on-rails ruby devise