首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionController::RoutingError:没有与{:period_registration=>{},:controller=>"period_registrations",:action=>"save_period"}匹配的路由

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

Stack Overflow用户
提问于 2012-08-23 02:14:20
回答 1查看 200关注 0票数 0

我有一个嵌套的路由:

代码语言:javascript
复制
resources :period_registrations do
  member do
    post :save_period
  end

它指向我的控制器操作:

代码语言:javascript
复制
 def save_period
    @period_registration = PeriodRegistration.new(params[:registration])
    @period_registration.save
    redirect_to root_path
  end

我有个测试:

代码语言:javascript
复制
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

运行时会生成以下错误:

代码语言:javascript
复制
 1) Error:
test_should_get_save_period(PeriodRegistrationsControllerTest):
ActionController::RoutingError: No route matches {:period_registration=>{}, :controller=>"period_registrations", :action=>"save_period"}

在我看来奇怪的是:period_registration是空的。应该是这样吗?我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-23 02:44:27

应该为collection定义post,即您需要更改路由:

代码语言:javascript
复制
post :save_period, :on => :collection

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

其他注意事项:

你有一个错误在你的控制器中:should post save_period,但应该是

  1. 中有一个拼写错误:should get save_period =>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12079027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档