[英]ActionController::RoutingError: No route matches {:period_registration=>{}, :controller=>“period_registrations”, :action=>“save_period”}
I have a nested route: 我有一条嵌套路线:
resources :period_registrations do
member do
post :save_period
end
that points to my controller action: 指向我的控制器动作:
def save_period
@period_registration = PeriodRegistration.new(params[:registration])
@period_registration.save
redirect_to root_path
end
and I have a test: 我有一个测试:
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
That when run generates the following error: 在运行时会产生以下错误:
1) Error:
test_should_get_save_period(PeriodRegistrationsControllerTest):
ActionController::RoutingError: No route matches {:period_registration=>{}, :controller=>"period_registrations", :action=>"save_period"}
What looks odd to me is that :period_registration is empty. 在我看来奇怪的是:period_registration为空。 Should it be? 应该是吗 How can I solve this? 我该如何解决?
post
should be defined for collection
, ie you need to change your routing: 应该为collection
定义post
,即您需要更改路由:
post :save_period, :on => :collection
instead of member
block. 而不是member
块。 As an example, rails build-in create
(generated by the resources
) method also binded to a collection. 例如,rails内置的create
(由resources
生成)方法也绑定到集合。
Additional notes: 补充笔记:
PeriodRegistration.new(params[:registration])
, but should be PeriodRegistration.new(params[:period_registration])
. 您的控制器有一个错误: PeriodRegistration.new(params[:registration])
,但应该是PeriodRegistration.new(params[:period_registration])
。 should get save_period
=> should post save_period
测试中有一个错字: should get save_period
=> should post save_period
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.