2012-08-22 63 views
0

我有一個嵌套的路線:的ActionController :: RoutingError:沒有路由匹配{:period_registration => {},:控制器=> 「period_registrations」:行動=> 「save_period」}

resources :period_registrations do 
    member do 
    post :save_period 
    end 

該點我的控制器操作:

def save_period 
    @period_registration = PeriodRegistration.new(params[:registration]) 
    @period_registration.save 
    redirect_to root_path 
    end 

和我有一個測試:

test "should get save_period" do 
    sign_in(FactoryGirl.create(:user)) 
    assert_difference('Event.count') do 
     post :save_period, period_registration: FactoryGirl.attributes_for(:period_registration) 
    end 
    assert_not_nil assigns(:period_registration) 

    assert_response :success 
    end 

運行時生成以下錯誤:

1) Error: 
test_should_get_save_period(PeriodRegistrationsControllerTest): 
ActionController::RoutingError: No route matches {:period_registration=>{}, :controller=>"period_registrations", :action=>"save_period"} 

我看起來很奇怪的是:period_registration是空的。它應該是?我該如何解決這個問題?

回答

1

post應該collection定義,即你需要改變你的路由:

post :save_period, :on => :collection 

,而不是member塊。例如,軌道內置create(由resources生成)方法也綁定到集合。

其他注意事項:

  1. 您有一個錯誤在你的控制器:PeriodRegistration.new(params[:registration]),而應該是PeriodRegistration.new(params[:period_registration])
  2. 並且在測試一個錯字:should get save_period =>should post save_period
+0

所以我需要做的就是添加'後:save_period,:上=>:collection'正確嗎? –

相關問題