[英]Routing custom action in Rails 3
I have a very simple question. 我有一个非常简单的问题。 Trying to figure out what is the simplest way to route the custom action in rails 3.
试图弄清楚在rails 3中路由自定义操作的最简单方法是什么。
Let's say i have controller UsersController
and action promote_to_premium
假设我有控制器
UsersController
和action promote_to_premium
Nor 也不
http://localhost:3000/users/#{user_id}/promote_to_premium
neither 也不
http://localhost:3000/users/promote_to_premium/#{user_id}
works. 作品。
Should I specify in routes.rb every custom action that differs from new/delete/update/create/ect/....????? 我应该在routes.rb中指定每个与new / delete / update / create / ect / ....不同的自定义操作吗?
Thank You. 谢谢。
Yes you need to specify in your routes.rb. 是的,您需要在routes.rb中指定。
Example: 例:
resources :users do
member do
post :promote_to_premium
end
end
This way you can access the route like this: 这样你就可以像这样访问这条路线:
http://localhost:3000/users/#{user_id}/promote_to_premium
你应该在routes.rb中使用它:
match "/users/:id/promote_to_premium" => "users#promote_to_premium"
You should mention the route in routes.rb file for custom methods in the controller. 您应该在routes.rb文件中提及控制器中自定义方法的路由。
You can specify the routes using either get""
or a match""=>""
or a "post"
您可以使用
get""
或match""=>""
或"post"
指定路线
when you write get "controller/something"
something should be an action(method) called by the name "something" in your controller. 当你写“得到
"controller/something"
某些东西应该是你的控制器中名称为“something”的一个动作(方法)。 But in your case you cannot use get"controller/:id"
as there is no ":id"
method in your controller. 但在你的情况下你不能使用
get"controller/:id"
因为你的控制器中没有":id"
方法。 So, you should match
your controller/:id
to some 'action'
in your controller. 因此,您应该将
controller/:id
match
controller/:id
中的某些'action'
match
。 Hence you need to write 因此你需要写
"match users/:id/promote_to_premium"=> "users#promote_to_premium"
But if you are writing something into the database then you should use ' post
'. 但是如果你在数据库中写东西,那么你应该使用'
post
'。 From whatever i know, i think you can try 从我所知道的,我想你可以试试
match 'users/:id/promote_to_premium' => 'users#promote_to_premium', :via => :post
You can study more about routes in the below link: http://guides.rubyonrails.org/routing.html 您可以在以下链接中了解有关路线的更多信息: http : //guides.rubyonrails.org/routing.html
Yes you need to specify every route. 是的,您需要指定每条路线。 Actually you define the normal routes too with the
resource
command. 实际上,您也可以使用
resource
命令定义正常路由。
There is a specific wildcard command to allow access of any action, but it is only for debug purposes, because it allows access to actions you may not want to be accessible: 有一个特定的通配符命令允许访问任何操作,但它仅用于调试目的,因为它允许访问您可能不希望访问的操作:
match ':controller(/:action(/:id(.:format)))'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.