简体   繁体   English

如何在Rails中只允许POST请求?

[英]How to only allow POST Requests in Rails?

How do I prevent users from calling a method "doAction" in my controller using GET requests? 如何阻止用户使用GET请求在我的控制器中调用方法“doAction”? I only want it to be called using POST requests? 我只想用POST请求调用它?

I heard I need to use "verify" but I'm unsure where to put that code, or how I need to use it. 我听说我需要使用“验证”,但我不确定在哪里放置代码,或者我需要如何使用它。

You can specify which methods are allowed for any action in your routes.rb . 您可以指定routes.rb任何操作允许的方法。

Rails 2: Rails 2:

map.connect '/posts/doAction', :controller => 'posts,
                               :action     => 'doAction',
                               :conditions => { :method => :post }

Rails 3: Rails 3:

match 'posts/doAction' => "posts#doAction', :via => :post

post 'posts/doAction', :to => "posts#doAction'

You can add a constraint to the route in routes.rb . 您可以在routes.rb为路径添加约束。

match "/doAction" => "controller#doAction", :via => :post

Refer to http://guides.rubyonrails.org/routing.html for more. 有关更多信息,请参阅http://guides.rubyonrails.org/routing.html

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

相关问题 如何只允许帖子的作者编辑或在Ruby on Rails中删除它? - How to allow only the author of a post edit or delete it in Ruby on Rails? 如何处理或防止Ruby on Rails中的“仅允许发布请求”错误? - How to handle or prevent “Only post requests are allowed” error in Ruby on Rails? 在 ruby​​ on rails 如何允许用户仅在周日(一天)创建帖子 工作日的其余时间不允许创建帖子 - In ruby on rails How to allow users to Create a post only Sunday(one day) rest of the weekday make create post not allow Rails RESTful API +仅支持GET / POST的客户端支持-有可能吗? - Rails RESTful API + support for clients that only allow GET/POST - Is it possible? 引用仅接受发布请求的Rails路线? - Reference a Rails route which only accepts post requests? Rails-操作方法-仅当存在布尔值时才允许登录 - Rails - How to - Allow login only if a boolean is present Rails POST请求 - Rails POST requests 如何编辑.htaccess以允许rails和wordpress请求? - How do I edit .htaccess to allow both rails and wordpress requests? Rails:如何只允许用户一次申请工作? - Rails: How to only allow User to apply to job only once? 如何以安全的方式将POST请求发送到Rails应用程序? - How can I send POST requests in a secure manner to a Rails application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM