【发布时间】:2016-03-15 16:12:52
【问题描述】:
在transactions 控制器(Rails 4.2)中定义了一个action transfer。操作transfer 与new 类似,通过弹出一个表单transfer.html.erb 供用户填写和保存:
def transfer
@title = t('Account Transfer')
@transaction = BankAccountx::Transaction.new()
end
在 routes.rb 中,定义路由:
resources :transactions do
member do
get :transfer
patch :transfer_result
end
end
这是规格:
it "should render transfer" do
session[:user_id] = @u.id
get 'transfer'
expect(response).to be_success
end
在rspec中,有错误:
ActionController::UrlGenerationError:
No route matches {:action=>"transfer", :controller=>"bank_accountx/transactions"}
规范中有以下内容:
routes {BankAccountx::Engine.routes}
如果我们在 routes.rb 中使用 collection 而不是 member,那么上面的 rspec 就会通过。以下路线有效:
resources :transactions do
collection do
get :transfer
patch :transfer_result
end
end
为什么这个成员操作transfer 需要routes.rb 中的收集路由?
【问题讨论】:
标签: ruby-on-rails rspec