我有以下
brokers_controller.rb
class BrokersController < ApplicationController
before_action :authenticate_broker!
before_action :set_broker, only: [:show, :edit, :update, :approve, :destroy]
# GET /brokers
# GET /brokers.json
def index
if current_user && current_user.admin
@brokers = Broker.all
else
@brokers = Broker.where(approved: true)
end
end
# GET /brokers/1
# GET /brokers/1.json
def show
end
# GET /brokers/new
def new
redirect_to brokers_path # brokers can't create brokers
@broker = Broker.new
end
# GET /brokers/1/edit
def edit
end
# POST /brokers
# POST /brokers.json
def create
redirect_to brokers_path
return
# @broker = Broker.new(broker_params)
#
# respond_to do |format|
# if @broker.save
# format.html { redirect_to @broker, notice: 'Broker was successfully created.' }
# format.json { render :show, status: :created, location: @broker }
# else
# format.html { render :new }
# format.json { render json: @broker.errors, status: :unprocessable_entity }
# end
# end
end
brokers_controller_test.rb
require 'test_helper'
class BrokersControllerTest < ActionDispatch::IntegrationTest
include Warden::Test::Helpers
setup do
@broker = brokers(:one)
login_as(@broker, :scope => :broker)
end
teardown do
Warden.test_reset!
end
test "should get index" do
get brokers_url
assert_response :success
end
test "should get new" do
get new_broker_url
assert_response :redirect
assert_redirected_to brokers_url
end
test "should create broker" do
# assert_no_difference('Broker.count') do
post brokers_url, params: { broker: { email: @broker.email, name: @broker.name } }
# end
assert_redirected_to brokers_url
end
日志
>rails test test/controllers/brokers_controller_test.rb:26
Run options: --seed 52623
# Running:
F
Failure:
BrokersControllerTest#test_should_create_broker [C:/Users/Chloe/workspace/test/controllers/brokers_controller_test.rb:31]:
Expected response to be a redirect to <http://www.example.com/brokers> but was a redirect to <http://www.example.com/>.
Expected "http://www.example.com/brokers" to be === "http://www.example.com/".
bin/rails test test/controllers/brokers_controller_test.rb:26
它会一直重定向到root,但我看不出原因。 “应该得到索引”和“应该得到新的”测试工作没问题。
答案 0 :(得分:1)
哦,我知道它是什么。我对此进行了评论,以便在浏览器中查看它
def new
# redirect_to brokers_path # brokers can't create brokers
它像警报
一样重定向到root您已登录。
日志显示
Started POST "/brokers" for ::1 at 2017-06-28 14:06:02 -0400
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"d4OoCcx5PmjnwOnpHfFOBvsAzAN52BExoOCstC9u/9sVLaithTbFPSIgCFlqILL5n2xTRYni1ZchB62EPKFVYg==", "broker"=>{"name"=>"XXX", "email"=>"xxx@example.com"}, "commit"=>"Save"}
Broker Load (3.0ms) SELECT "brokers".* FROM "brokers" WHERE "brokers"."id" = $1 ORDER BY "brokers"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 6ms (ActiveRecord: 3.0ms)
关键是由Devise处理:: RegistrationsController #create 。当我查看路线时,我看到了
>rake routes | grep brokers
...
new_broker_registration GET /brokers/sign_up(.:format) devise/registrations#new
...
broker_registration PATCH /brokers(.:format) devise/registrations#update
PUT /brokers(.:format) devise/registrations#update
DELETE /brokers(.:format) devise/registrations#destroy
POST /brokers(.:format) devise/registrations#create
...
brokers GET /brokers(.:format) brokers#index
POST /brokers(.:format) brokers#create
Devise的路线来自控制器。
Rails.application.routes.draw do
devise_for :brokers
...
resources :brokers do
因此,当它发布到/ Broker时,它首先找到Devise。我认为Devise会POST到/ brokers / sign_up。