【问题标题】:Devise DoubleRenderError设计 DoubleRenderError
【发布时间】:2010-06-20 17:03:04
【问题描述】:

应用程序堆栈:Rails3、CanCan、Devise、Shouda

我有一些嵌套资源,想用 Shoulda 测试它们,我得到以下 DoubleRenderError:

Error:
test: If anonymous user tries to GET index of fav_blogs should respond with 401.    (BlogItemsControllerTest):
AbstractController::DoubleRenderError: Render and/or redirect were called multiple times  in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

测试将检查未经身份验证的用户是否能够访问嵌套资源(他不应该)

测试方法

context "If anonymous user tries to GET index of fav_blogs" do
  setup do
    get :index, :end_user_id => end_users(:end_user_one).id, :format => "xml"
  end
  should_respond_with 401
  should_not_assign_to :blog_items
end

控制器方法:

def index

  if params[:end_user_id] # if it is nested
    authenticate_end_user!
    authorize! :read, @end_user

    @blog_items = @end_user.blog_items
  else
    @PAGE_SIZE = 10
    page = Integer(params[:page]) || 0

    offset = @PAGE_SIZE * page
    @blog_items = BlogItem.limit( offset + @PAGE_SIZE ).offset( offset ).order('updated_at DESC').all
  end

  respond_with(@blog_items)
end

使用经过身份验证的用户进行的所有测试都可以正常工作 - 可能有人可以给我一个提示。非常感谢!本

【问题讨论】:

    标签: ruby-on-rails-3 shoulda devise cancan


    【解决方案1】:

    好的,我已经完成了 - 出现问题是因为

    authenticate_end_user!
    authorize! :read, @end_user
    

    块。如果没有授权,第一行将呈现 401 失败,但无论如何,第二行将被执行。因此,将身份验证放在 before_filter 中,将授权放在另一个或您的控制器操作中。 本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2014-07-02
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多