自定义current_user设计+ Mongoid

时间:2011-10-03 16:18:58

标签: ruby-on-rails devise mongoid

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

这是我的 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

我在顶级house_controller中也有这个代码:

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

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

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

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

2 个答案:

答案 0 :(得分:1)

你需要把别的东西放在那里......你可能想写:

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

答案 1 :(得分:0)

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