提交 b7a09453 编写于 作者: X Xavier Noria

uses PATCH for the forms of persisted records, and routes PATCH and PUT to the...

uses PATCH for the forms of persisted records, and routes PATCH and PUT to the update action of resources
上级 31ceb5e6
## 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)
......
## Rails 4.0.0 (unreleased) ##
* New configuration option `config.default_method_for_update` tells Rails which
HTTP verb to use for update actions. Values can be `:patch` and `:put`.
Default is `:put` for backwards compatibility, but you are encouraged to
use `:patch` for proper HTTP semantics with partial updates.
* Allow to set class that will be used to run as a console, other than IRB, with `Rails.application.config.console=`. It's best to add it to `console` block. *Piotr Sarnacki*
Example:
......
......@@ -76,8 +76,6 @@ NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is
* +config.consider_all_requests_local+ is a flag. If true then any error will cause detailed debugging information to be dumped in the HTTP response, and the +Rails::Info+ controller will show the application runtime context in +/rails/info/properties+. True by default in development and test environments, and false in production mode. For finer-grained control, set this to false and implement +local_request?+ in controllers to specify which requests should provide debugging information on errors.
* +config.default_method_for_update+ tells Rails which HTTP method to use for update actions by default. Set this to +:patch+ for proper HTTP update semantics. The default is +:put+ for backwards compatibility.
* +config.dependency_loading+ is a flag that allows you to disable constant autoloading setting it to false. It only has effect if +config.cache_classes+ is true, which it is by default in production mode. This flag is set to false by +config.threadsafe!+.
* +config.eager_load_paths+ accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the +app+ directory of the application.
......
......@@ -292,7 +292,7 @@ form_for(@article)
## Editing an existing article
# long-style:
form_for(@article, :url => article_path(@article), :html => { :method => "put" })
form_for(@article, :url => article_path(@article), :html => { :method => "patch" })
# short-style:
form_for(@article)
</ruby>
......@@ -303,11 +303,6 @@ Rails will also automatically set the +class+ and +id+ of the form appropriately
WARNING: When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, +:url+, and +:method+ explicitly.
NOTE: Rails can use the +PATCH+ method instead of +PUT+ for update forms if you set the following option in your +config/application.rb+:
<ruby>
config.default_method_for_update = :patch
</ruby>
h5. Dealing with Namespaces
If you have created namespaced routes, +form_for+ has a nifty shorthand for that too. If your application has an admin namespace then
......
......@@ -85,7 +85,7 @@ creates seven different routes in your application, all mapping to the +Photos+
|PUT/PATCH |/photos/:id |update |update a specific photo |
|DELETE |/photos/:id |destroy |delete a specific photo |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. Rails routes are matched in the order they are specified, so if you have a +resources :photos+ above a +get 'photos/poll'+ the +show+ action's route for the +resources+ line will be matched before the +get+ line. To fix this, move the +get+ line *above* the +resources+ line so that it is matched first.
NOTE: Rails routes are matched in the order they are specified, so if you have a +resources :photos+ above a +get 'photos/poll'+ the +show+ action's route for the +resources+ line will be matched before the +get+ line. To fix this, move the +get+ line *above* the +resources+ line so that it is matched first.
h4. Paths and URLs
......@@ -140,7 +140,7 @@ creates six different routes in your application, all mapping to the +Geocoders+
|PUT/PATCH |/geocoder |update |update the one and only geocoder resource |
|DELETE |/geocoder |destroy |delete the geocoder resource |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. Because you might want to use the same controller for a singular route (+/account+) and a plural route (+/accounts/45+), singular resources map to plural controllers.
NOTE: Because you might want to use the same controller for a singular route (+/account+) and a plural route (+/accounts/45+), singular resources map to plural controllers.
A singular resourceful route generates these helpers:
......@@ -171,8 +171,6 @@ This will create a number of routes for each of the +posts+ and +comments+ contr
|PUT/PATCH |/admin/posts/:id |update | admin_post_path(:id) |
|DELETE |/admin/posts/:id |destroy | admin_post_path(:id) |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+.
If you want to route +/posts+ (without the prefix +/admin+) to +Admin::PostsController+, you could use
<ruby>
......@@ -212,8 +210,6 @@ In each of these cases, the named routes remain the same as if you did not use +
|PUT/PATCH |/admin/posts/:id |update | post_path(:id) |
|DELETE |/admin/posts/:id |destroy | post_path(:id) |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+.
h4. Nested Resources
It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
......@@ -247,8 +243,6 @@ In addition to the routes for magazines, this declaration will also route ads to
|PUT/PATCH |/magazines/:id/ads/:id |update |update a specific ad belonging to a specific magazine |
|DELETE |/magazines/:id/ads/:id |destroy |delete a specific ad belonging to a specific magazine |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+.
This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+. These helpers take an instance of Magazine as the first parameter (+magazine_ads_url(@magazine)+).
h5. Limits to Nesting
......@@ -693,8 +687,6 @@ will recognize incoming paths beginning with +/photos+ and route the requests to
|PUT/PATCH |/photos/:id |update | image_path(:id) |
|DELETE |/photos/:id |destroy | image_path(:id) |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+.
h4. Overriding the +new+ and +edit+ Segments
The +:path_names+ option lets you override the automatically-generated "new" and "edit" segments in paths:
......@@ -799,8 +791,6 @@ Rails now creates routes to the +CategoriesController+.
|PUT/PATCH |/kategorien/:id |update | category_path(:id) |
|DELETE |/kategorien/:id |destroy | category_path(:id) |
NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+.
h4. Overriding the Singular Form
If you want to define the singular form of a resource, you should add additional rules to the +Inflector+.
......
......@@ -11,7 +11,7 @@ class Configuration < ::Rails::Engine::Configuration
:force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks,
:railties_order, :relative_url_root, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change, :default_method_for_update
:time_zone, :reload_classes_only_on_change
attr_writer :log_level
attr_reader :encoding
......@@ -40,7 +40,6 @@ def initialize(*)
@reload_classes_only_on_change = true
@file_watcher = ActiveSupport::FileUpdateChecker
@exceptions_app = nil
@default_method_for_update = :put
@autoflush_log = true
@assets = ActiveSupport::OrderedOptions.new
......
......@@ -31,9 +31,6 @@ class Application < Rails::Application
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Use PATCH as default method for update actions
# config.default_method_for_update = :patch
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
......
......@@ -268,7 +268,6 @@ def update
RUBY
add_to_config <<-RUBY
config.default_method_for_update = :patch
routes.prepend do
resources :posts
end
......@@ -276,9 +275,6 @@ def update
require "#{app_path}/config/environment"
assert_equal ActionView::Base.default_method_for_update, :patch
assert_equal ActionDispatch::Routing::Mapper.default_method_for_update, :patch
get "/posts/1"
assert_match /patch/, last_response.body
......
......@@ -325,11 +325,6 @@ def test_no_active_record_or_test_unit_if_skips_given
assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
end
def test_default_method_for_update_is_not_patch
run_generator [destination_root, "--skip-test-unit", "--skip-active-record"]
assert_file "config/application.rb", /#\s+config\.default_method_for_update\s+=\s+:patch/
end
def test_new_hash_style
run_generator [destination_root]
assert_file "config/initializers/session_store.rb" do |file|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册