Permalink
Browse files

uses PATCH for the forms of persisted records, and routes PATCH and P…

…UT to the update action of resources
  • Loading branch information...
1 parent 31ceb5e commit b7a094536de6fdeb428aa79d21f1e2128169f45e @fxn fxn committed Feb 25, 2012
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Forms of persisted records use always PATCH (via the `_method` hack). *fxn*
+
+* For resources, both PATCH and PUT are routed to the `update` action. *fxn*
+
* Don't ignore `force_ssl` in development. This is a change of behavior - use a `:if` condition to recreate the old behavior.
class AccountsController < ApplicationController
@@ -23,7 +23,6 @@ class Railtie < Rails::Railtie
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
ActionDispatch::Response.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
- ActionDispatch::Routing::Mapper.default_method_for_update = app.config.default_method_for_update
ActionDispatch::ExceptionWrapper.rescue_responses.merge!(config.action_dispatch.rescue_responses)
ActionDispatch::ExceptionWrapper.rescue_templates.merge!(config.action_dispatch.rescue_templates)
@@ -7,8 +7,6 @@
module ActionDispatch
module Routing
class Mapper
- cattr_accessor(:default_method_for_update) {:put}
-
class Constraints #:nodoc:
def self.new(app, constraints, request = Rack::Request)
if constraints.any?
@@ -1012,10 +1010,11 @@ def resource(*resources, &block)
end if parent_resource.actions.include?(:new)
member do
- get :edit if parent_resource.actions.include?(:edit)
- get :show if parent_resource.actions.include?(:show)
+ get :edit if parent_resource.actions.include?(:edit)
+ get :show if parent_resource.actions.include?(:show)
if parent_resource.actions.include?(:update)
- send default_method_for_update, :update
+ patch :update
+ put :update
end
delete :destroy if parent_resource.actions.include?(:destroy)
end
@@ -1152,12 +1151,12 @@ def resources(*resources, &block)
get :new
end if parent_resource.actions.include?(:new)
- # TODO: Only accept patch or put depending on config
member do
- get :edit if parent_resource.actions.include?(:edit)
- get :show if parent_resource.actions.include?(:show)
+ get :edit if parent_resource.actions.include?(:edit)
+ get :show if parent_resource.actions.include?(:show)
if parent_resource.actions.include?(:update)
- send default_method_for_update, :update
+ patch :update
+ put :update
end
delete :destroy if parent_resource.actions.include?(:destroy)
end
@@ -132,8 +132,6 @@ module ActionView #:nodoc:
class Base
include Helpers, ::ERB::Util, Context
- cattr_accessor(:default_method_for_update) {:put}
-
# Specify the proc used to decorate input tags that refer to attributes with errors.
cattr_accessor :field_error_proc
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
@@ -385,7 +385,7 @@ def apply_form_for_options!(record, object, options) #:nodoc:
object = convert_to_model(object)
as = options[:as]
- action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, ActionView::Base.default_method_for_update] : [:new, :post]
+ action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post]
options[:html].reverse_merge!(
:class => as ? "#{action}_#{as}" : dom_class(object, action),
:id => as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence,
@@ -34,7 +34,6 @@ class Railtie < Rails::Railtie
initializer "action_view.set_configs" do |app|
ActiveSupport.on_load(:action_view) do
- self.default_method_for_update = app.config.default_method_for_update
app.config.action_view.each do |k,v|
send "#{k}=", v
end
@@ -1026,6 +1026,10 @@ def test_resource_routes_only_create_update_destroy
assert_equal 'pasts#destroy', @response.body
assert_equal '/past', past_path
+ patch '/present'
+ assert_equal 'presents#update', @response.body
+ assert_equal '/present', present_path
+
put '/present'
assert_equal 'presents#update', @response.body
assert_equal '/present', present_path
@@ -1044,6 +1048,10 @@ def test_resources_routes_only_create_update_destroy
assert_equal 'relationships#destroy', @response.body
assert_equal '/relationships/1', relationship_path(1)
+ patch '/friendships/1'
+ assert_equal 'friendships#update', @response.body
+ assert_equal '/friendships/1', friendship_path(1)
+
put '/friendships/1'
assert_equal 'friendships#update', @response.body
assert_equal '/friendships/1', friendship_path(1)
Oops, something went wrong. Retry.

0 comments on commit b7a0945

Please sign in to comment.