Rails 2 到 Rails 3,控制器中的方法验证消失了吗? [英] Rails 2 to Rails 3, method verification in controllers gone?

查看:21
本文介绍了Rails 2 到 Rails 3,控制器中的方法验证消失了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 rails 2,我的大多数控制器都会有以下几行:

Coming from rails 2, most of my controllers would have these lines:

verify :method => :post, :only => :create, :render => {:text => '405 HTTP POST required', :status => 405}, :add_headers => {'Allow' => 'POST'}
verify :method => :put, :only => :update, :render => {:text => '405 HTTP PUT required', :status => 405}, :add_headers => {'Allow' => 'PUT'}
verify :method => :delete, :only => :destroy, :render => {:text => '405 HTTP DELETE required', :status => 405}, :add_headers => {'Allow' => 'DELETE'}

迁移到 Rails 3 后,我收到弃用警告,告诉我这些已被删除.我知道我可以得到一些插件或其他东西来继续使用它们,但我的问题是我真的需要吗?默认情况下,rails 3 是否强制执行基本方法?好像应该可以,如果不行,我总觉得要一遍又一遍地写这些台词很烦...

After migrating to Rails 3, I get the deprecation warning telling me that these have been removed. I know I can get some plugin or whatever to still use them, but my question is do I really need to anymore? Does rails 3 enforce the basic methods by default? Seems like it should if it doesnt, I always felt annoyed to have to write these lines over and over again...

推荐答案

如果使用 REST 路由,则不需要验证请求方法.使用错误的请求方法根本无法访问控制器操作.

You don't need to verify the request method if you use REST routes. The controller action simply won't be reach with the wrong request method.

例如,如果您尝试通过 get 访问/users/create?name=my_name,则请求将使用 params[:id] = create 到达 show 操作,并且会失败.

For example, if you try to reach /users/create?name=my_name through get, the request will reach the show action with the params[:id] = create, and that will fail.

这篇关于Rails 2 到 Rails 3,控制器中的方法验证消失了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆