简体   繁体   English

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

[英]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: 补充笔记:

  1. You have an error in your controller: PeriodRegistration.new(params[:registration]) , but should be PeriodRegistration.new(params[:period_registration]) . 您的控制器有一个错误: PeriodRegistration.new(params[:registration]) ,但应该是PeriodRegistration.new(params[:period_registration])
  2. And there is a typo in test: should get save_period => should post save_period 测试中有一个错字: should get save_period => should post save_period

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActionController :: RoutingError:没有路由与{:controller =>“ vendors”,:action =>“ vendors”}匹配? - ActionController::RoutingError: No route matches {:controller=>“vendors”, :action=>“vendors”}? ActionController :: RoutingError(没有路由与{:action =>“ show”,:controller =>“ users”,:id => nil}匹配): - ActionController::RoutingError (No route matches {:action=>“show”, :controller=>“users”, :id=>nil}): ActionController :: RoutingError(没有路由匹配{:controller =>“users”,:action =>“profile”}) - ActionController::RoutingError (No route matches {:controller=>“users”, :action=>“profile”}) ActionController :: RoutingError-没有路由匹配 - ActionController::RoutingError - No route matches ActionController :: RoutingError(没有路由匹配 - ActionController::RoutingError (No route matches 设计命名空间 - ActionController :: RoutingError(无路由匹配{:action =>“new”,:controller =>“devise / sessions”}) - Devise in namespace - ActionController::RoutingError (No route matches {:action=>“new”, :controller=>“devise/sessions”}) test_should_get_show(CartsControllerTest):ActionController :: RoutingError:没有路线匹配{:cart =>“ 1”,:controller =>“ carts”,:action =>“ show”} - test_should_get_show(CartsControllerTest): ActionController::RoutingError: No route matches {:cart=>“1”, :controller=>“carts”, :action=>“show”} ActionController :: RoutingError(没有路由与[GET]匹配 - ActionController::RoutingError (No route matches [GET] " ActionController :: RoutingError(没有路线与[DELETE]匹配 - ActionController::RoutingError (No route matches [DELETE] ActionController :: RoutingError没有路由匹配[DELETE] - ActionController::RoutingError No route matches [DELETE]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM