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

查看:157
本文介绍了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.

例如,如果您尝试访问/ users / create?name = my_name通过get,请求将使用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天全站免登陆