ActionController :: RoutingError:没有路由匹配{:period_registration => {},:controller =>“period_registrations”,:action =>“save_period”}

时间:2012-08-22 18:14:20

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

我有一个嵌套路线:

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 个答案:

答案 0 :(得分:1)

应为post定义

collection,即您需要更改路由:

post :save_period, :on => :collection

而不是member阻止。例如,rails内置create(由resources生成)方法也绑定到集合。

附加说明:

  1. 您的控制器中出现错误:PeriodRegistration.new(params[:registration]),但应为PeriodRegistration.new(params[:period_registration])
  2. 测试中有一个拼写错误:should get save_period => should post save_period