【问题标题】:Permission in railsrails中的权限
【发布时间】:2014-09-25 13:18:18
【问题描述】:

我有一个工作页面,用户可以在其中创建新工作编辑和销毁他们的工作,我想让用户仅在连接时编辑或销毁他们的帖子,否则他们将返回到工作显示页面我有这个代码在我的工作控制器中

def require_login
  @job = current_user.jobs.find_by_slug(params[:id])
  redirect_to job_path if @job.nil?
end

  before_action :login_required
  def login_required
    redirect_to new_user_session_path unless user_signed_in?
  end
  before_action :login_required, :require_login, only: [:edit, :update, :destroy]

这不起作用的唯一部分是当我没有连接并尝试编辑我的工作时,它会将我重定向到登录表单,但登录后它将我重定向到主页而不是编辑页面

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 permissions


    【解决方案1】:

    首先,您粘贴的代码块看起来很奇怪,原因有三个:

    1. 缩进错误
    2. 你有两次before_action :login_required,甚至不一样
    3. 从方法名称中不清楚您到底想要什么

    根据您所写的内容,您希望用户仅在登录时编辑或销毁工作 - 否则您希望将他们发送到工作索引页面。如果正确,您控制器中的代码应该如下所示

    class JobsController < ApplicationController
      before_filter :require_login, :only => [:edit, :update, :destroy]
    
      def edit
        # your code here
      end
    
      def update
        # your code here
      end
    
      def destroy
        # your code here
      end
    
    private
      def require_login
        redirect_to job_path unless user_signed_in?
      end
    end
    

    【讨论】:

    • 我对这个答案投了反对票,但没有解释原因,非常感谢您的评论。
    猜你喜欢
    • 2023-04-02
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 2015-10-30
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多