如何在没有 CRUD 操作的情况下路由控制器? [英] How to route controllers without CRUD actions?
本文介绍了如何在没有 CRUD 操作的情况下路由控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个有很多动作的控制器:
I have a controller with a number of actions:
class TestsController < ApplicationController
def find
end
def break
end
def turn
end
end
当我像这样将它添加到我的 routes.rb
文件时:
When I add it to my routes.rb
file like so:
resources :tests
并执行 rake routes
任务我看到以下额外的回合:
and execute the rake routes
task I see the following extra rounds:
tests GET /tests(.:format) tests#index
POST /tests(.:format) tests#create
new_test GET /tests/new(.:format) tests#new
edit_test GET /tests/:id/edit(.:format) tests#edit
test GET /tests/:id(.:format) tests#show
PUT /tests/:id(.:format) tests#update
DELETE /tests/:id(.:format) tests#destroy
显然我的控制器没有上述操作.那么我如何告诉 Rails 避免生成/期望这些路由?
Obviously my controller doesn't have the above actions. So how do I tell Rails to avoid generating/expecting those routes?
推荐答案
您可以像这样指定要路由的操作:
You can specify actions you want to route like this:
resources :tests, except: [:new, :create, :edit, :update, :destroy] do
collection do
get 'find'
get 'break'
get 'turn'
end
end
这篇关于如何在没有 CRUD 操作的情况下路由控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文