Rails 2 to Rails 3, method verification in controllers gone?

雨燕双飞 提交于 2019-12-04 11:47:13

问题


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'}

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...


回答1:


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.

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.



来源:https://stackoverflow.com/questions/3707071/rails-2-to-rails-3-method-verification-in-controllers-gone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!