Разрабатывать Дутерэндерэррор
-
28-09-2019 - |
Вопрос
Application-Stack: Rails3, Cancan, Devise, Holda
У меня есть несколько вложенных ресурсов и хотят проверить их с помощью HONA, и я получаю следующую Дутерэндерэрор:
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
ControlMermethod:
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
Все тесты с аутентифицированным пользователем работают нормально - пусть кто-то может дать мне намек. Большое спасибо! Бен
Решение
Хорошо, я закончил - проблема возникает из-за
authenticate_end_user!
authorize! :read, @end_user
блокировать. Первая строка идет рендер 401 сбой в случае отсутствия авторизации, но иначе, вторая строка будет выполнена. Следовательно, поместите аутентификацию в REPE_FILTER и авторизацию в другом или действию вашего контроллера. Бен
Не связан с StackOverflow