Rspec:为什么我的帖子:创建抛出:找不到新路由错误?

时间:2014-12-02 20:58:47

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

在像这样的小Rspec测试中:

describe 'POST create' do
  context 'with valid attributes' do
    @document = FactoryGirl.attributes_for(:document_with_publication)
    it 'creates a new document' do
      sign_in admin
      post :create, document: @document
      expect(response).to change(Document, :count).by(1)
    end
  end
end

我收到此错误:

DocumentsController user is an administrator POST create with valid attributes creates a new document
 Failure/Error: post :create, document: @document
 ActionController::RoutingError:
   No route matches {:controller=>"documents", :action=>"new", :template=>nil}

为什么我似乎在点击:new行动,而不是:create?我的路线看起来像这样:

resources :documents, except: [:new, :show]
get 'documents/:template/new', to: 'documents#new', as: :new_templated_document

感谢。相关的控制器代码在这里:

def new
  @document = current_user.documents.new
  @document.template = Template.find(params[:template])
  @template_resources = TemplateResources.new(@document, current_user)
end

def create
  @document = current_user.documents.new(params[:document])
  if @document.save
    second_stage_processing
  else
    redirect_to new_templated_document_path(@document.template), flash:
    { error: 'The document you tried to create was invalid: ' \
             "#{@document.errors.full_messages.map { |msg| msg }.join}" }
  end
end

1 个答案:

答案 0 :(得分:1)

我认为它正在触及创建操作,而@document.save失败导致重定向到redirect_to new_templated_document_path(@document.template)被调用。因为:该路由需要模板,但是为零,路由失败。