【问题标题】:Custom current_user devise + Mongoid自定义 current_user 设计 + Mongoid
【发布时间】:2011-10-03 16:18:58
【问题描述】:

我已经设计 + 一个脚手架“房子”,我希望用户只能编辑你自己的房子。

这是我的houses_controller:

def authenticate_owner!
  @house = house.find(params[:id])
    if user_signed_in? && current_user.email == @house.user.email
      return true
    end
    redirect_to root_path, :notice => "You must have permission to access this category."
      return false
    end

我在顶级houses_controller 中也有这段代码:

before_filter :authenticate_owner!
skip_before_filter :authenticate_owner! , :only => [:show, :index, :new]

但不工作,总是显示消息:

“您必须有权访问此类别。”

如何获取创建脚手架的用户并将其与注册用户进行比较?

【问题讨论】:

  • 你贴的代码不完整

标签: ruby-on-rails devise mongoid


【解决方案1】:

你需要在那里放一个 else .. 你可能打算写:

def authenticate_owner!
  @house = house.find(params[:id])
    if user_signed_in? && current_user.email == @house.user.email
      return true
    else
      redirect_to root_path, :notice => "You must have permission to access this category."
      return false   # this will never be executed!!
    end
end

【讨论】:

    【解决方案2】:

    已修复:D 问题是@house = house.find(params[:id]),是:@house = House.find(params[:id ])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多