responders 2.0.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +66 -1
- data/MIT-LICENSE +2 -1
- data/README.md +150 -49
- data/lib/action_controller/respond_with.rb +52 -20
- data/lib/action_controller/responder.rb +44 -28
- data/lib/generators/rails/responders_controller_generator.rb +4 -26
- data/lib/generators/rails/templates/api_controller.rb.tt +51 -0
- data/lib/generators/rails/templates/controller.rb.tt +59 -0
- data/lib/generators/responders/install_generator.rb +23 -9
- data/lib/responders/collection_responder.rb +3 -0
- data/lib/responders/controller_method.rb +7 -2
- data/lib/responders/flash_responder.rb +32 -27
- data/lib/responders/http_cache_responder.rb +5 -3
- data/lib/responders/version.rb +3 -1
- data/lib/responders.rb +16 -16
- metadata +32 -57
- data/lib/generators/rails/templates/controller.rb +0 -55
- data/lib/responders/location_responder.rb +0 -8
- data/test/action_controller/respond_with_test.rb +0 -717
- data/test/locales/en.yml +0 -28
- data/test/responders/collection_responder_test.rb +0 -82
- data/test/responders/controller_method_test.rb +0 -72
- data/test/responders/flash_responder_test.rb +0 -260
- data/test/responders/http_cache_responder_test.rb +0 -120
- data/test/test_helper.rb +0 -87
- data/test/views/addresses/create.js.erb +0 -1
- data/test/views/addresses/edit.html.erb +0 -1
- data/test/views/addresses/new.html.erb +0 -1
- data/test/views/locations/new.html.erb +0 -1
- data/test/views/respond_with/edit.html.erb +0 -1
- data/test/views/respond_with/new.html.erb +0 -1
- data/test/views/respond_with/respond_with_additional_params.html.erb +0 -0
- data/test/views/respond_with/using_invalid_resource_with_template.xml.erb +0 -1
- data/test/views/respond_with/using_options_with_template.xml.erb +0 -1
- data/test/views/respond_with/using_resource.js.erb +0 -1
- data/test/views/respond_with/using_resource_with_block.html.erb +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c070413f30e2321496c58e5c47349b4b62b4409ecb73552d0e58e71394d588fd
|
4
|
+
data.tar.gz: 3853489006f9236c38e2b3842a8c29e6cec2a47f3f919250f6d88e2b5029309d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6120b807812d05222247bd55464aaf762b00620cbd8b26877f2d9de38d096d9894f272c00a1f6c796247bbb6260ddeb45234b57947a4f596c9725f8bcf50a3b7
|
7
|
+
data.tar.gz: fd412c3c3c9dc33d66f2695c99180e98bbdc3dc6300481372068c5a3ae79e17a1c7f8d8f5e4eeddc9c7590de5e0996584e02e64728ccdb535ff8e19c529bf7b6
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,73 @@
|
|
1
|
+
## 3.1.1
|
2
|
+
|
3
|
+
* Add support for Rails 7.1. (no changes required.)
|
4
|
+
|
5
|
+
## 3.1.0
|
6
|
+
|
7
|
+
* Add config `responders.redirect_status` to allow overriding the redirect code/status used in redirects. The default is `302 Found`, which matches Rails, but it allows to change responders to redirect with `303 See Other` for example, to make it more compatible with how Hotwire/Turbo expects redirects to work.
|
8
|
+
* Add config `responders.error_status` to allow overriding the status code used to respond to `HTML` or `JS` requests that have errors on the resource. The default is `200 OK`, but it allows to change the response to be `422 Unprocessable Entity` in such cases for example, which makes it more consistent with other statuses more commonly used in APIs (like JSON/XML), and works by default with Turbo/Hotwire which expects a 422 on form error HTML responses. Note that changing this may break your application if you're relying on the previous 2xx status to handle error cases.
|
9
|
+
* Add support for Ruby 3.0, 3.1, and 3.2, drop support for Ruby < 2.5.
|
10
|
+
* Add support for Rails 6.1 and 7.0, drop support for Rails < 5.2.
|
11
|
+
* Move CI to GitHub Actions.
|
12
|
+
|
13
|
+
## 3.0.1
|
14
|
+
|
15
|
+
* Add support to Ruby 2.7
|
16
|
+
|
17
|
+
## 3.0.0
|
18
|
+
|
19
|
+
* Remove support for Rails 4.2
|
20
|
+
* Remove support for Ruby < 2.4
|
21
|
+
|
22
|
+
## 2.4.1
|
23
|
+
|
24
|
+
* Add support for Rails 6 beta
|
25
|
+
|
26
|
+
## 2.4.0
|
27
|
+
|
28
|
+
* `respond_with` now accepts a new kwarg called `:render` which goes straight to the `render`
|
29
|
+
call after an unsuccessful post request. Useful if for example you need to render a template
|
30
|
+
which is outside of controller's path eg:
|
31
|
+
|
32
|
+
`respond_with resource, render: { template: 'path/to/template' }`
|
33
|
+
|
34
|
+
## 2.3.0
|
35
|
+
|
36
|
+
* `verify_request_format!` is aliased to `verify_requested_format!` now.
|
37
|
+
* Implementing the `interpolation_options` method on your controller is deprecated
|
38
|
+
in favor of naming it `flash_interpolation_options` instead.
|
39
|
+
|
40
|
+
## 2.2.0
|
41
|
+
|
42
|
+
* Added the `verify_request_format!` method, that can be used as a `before_action`
|
43
|
+
callback to prevent your actions from being invoked when the controller does
|
44
|
+
not respond to the request mime type, preventing the execution of complex
|
45
|
+
queries or creating/deleting records from your app.
|
46
|
+
|
47
|
+
## 2.1.2
|
48
|
+
|
49
|
+
* Fix rendering when using `ActionController::API`. (by @eLod)
|
50
|
+
* Added API controller template for the controller generator. (by @vestimir)
|
51
|
+
|
52
|
+
## 2.1.1
|
53
|
+
|
54
|
+
* Added support for Rails 5.
|
55
|
+
|
56
|
+
## 2.1.0
|
57
|
+
|
58
|
+
* No longer automatically set the responders generator as many projects may use this gem as a dependency. When upgrading, users will need to add `config.app_generators.scaffold_controller :responders_controller` to their application. The `responders:install` generator has been updated to automatically insert it in new applications
|
59
|
+
|
60
|
+
## 2.0.1
|
61
|
+
|
62
|
+
* Require `rails/railtie` explicitly before using it
|
63
|
+
* Require `action_controller` explicitly before using it
|
64
|
+
* Remove unnecessary and limiting `resourceful?` check that required models to implement `to_#{format}` (such checks are responsibility of the rendering layer)
|
65
|
+
|
1
66
|
## 2.0.0
|
2
67
|
|
3
68
|
* Import `respond_with` and class-level `respond_to` from Rails
|
4
69
|
* Support only Rails ~> 4.2
|
5
|
-
* `Responders::LocationResponder` is now included by
|
70
|
+
* `Responders::LocationResponder` is now included by the default responder (and therefore deprecated)
|
6
71
|
|
7
72
|
## 1.1.0
|
8
73
|
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
Copyright
|
1
|
+
Copyright (c) 2020-2023 Rafael França, Carlos Antônio da Silva
|
2
|
+
Copyright (c) 2009-2019 Plataformatec
|
2
3
|
|
3
4
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
5
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -1,10 +1,23 @@
|
|
1
1
|
# Responders
|
2
2
|
|
3
|
-
[](http://travis-ci.org/plataformatec/responders)
|
5
|
-
[](https://codeclimate.com/github/plataformatec/responders)
|
3
|
+
[](http://badge.fury.io/rb/responders)
|
6
4
|
|
7
|
-
A set of responders modules to dry up your Rails
|
5
|
+
A set of responders modules to dry up your Rails app.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add the responders gem to your Gemfile:
|
10
|
+
|
11
|
+
gem "responders"
|
12
|
+
|
13
|
+
Update your bundle and run the install generator:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
$ rails g responders:install
|
17
|
+
|
18
|
+
If you are including this gem to support backwards compatibilty for responders in previous releases of Rails, you only need to include the gem and bundle.
|
19
|
+
|
20
|
+
$ bundle install
|
8
21
|
|
9
22
|
## Responders Types
|
10
23
|
|
@@ -60,77 +73,78 @@ You can also have embedded HTML. Just create a `_html` scope.
|
|
60
73
|
|
61
74
|
See also the `namespace_lookup` option to search the full hierarchy of possible keys.
|
62
75
|
|
63
|
-
###
|
76
|
+
### HttpCacheResponder
|
77
|
+
|
78
|
+
Automatically adds Last-Modified headers to API requests. This
|
79
|
+
allows clients to easily query the server if a resource changed and if the client tries
|
80
|
+
to retrieve a resource that has not been modified, it returns not_modified status.
|
81
|
+
|
82
|
+
### CollectionResponder
|
83
|
+
|
84
|
+
Makes your create and update action redirect to the collection on success.
|
85
|
+
|
86
|
+
### LocationResponder
|
64
87
|
|
65
88
|
This responder allows you to use callable objects as the redirect location.
|
66
89
|
Useful when you want to use the `respond_with` method with
|
67
90
|
a custom route that requires persisted objects, but the validation may fail.
|
68
91
|
|
92
|
+
Note: this responder is included by default, and doesn't need to be included
|
93
|
+
on the top of your controller (including it will issue a deprecation warning).
|
94
|
+
|
69
95
|
```ruby
|
70
96
|
class ThingsController < ApplicationController
|
71
|
-
responders :location, :flash
|
72
97
|
respond_to :html
|
73
98
|
|
74
99
|
def create
|
75
|
-
@thing =
|
100
|
+
@thing = Thing.create(params[:thing])
|
76
101
|
respond_with @thing, location: -> { thing_path(@thing) }
|
77
102
|
end
|
78
103
|
end
|
79
104
|
```
|
80
105
|
|
81
|
-
|
106
|
+
**Dealing with namespaced routes**
|
82
107
|
|
83
|
-
|
84
|
-
allows clients to easily query the server if a resource changed and if the client tries
|
85
|
-
to retrieve a resource that has not been modified, it returns not_modified status.
|
86
|
-
|
87
|
-
### CollectionResponder
|
88
|
-
|
89
|
-
Makes your create and update action redirect to the collection on success.
|
108
|
+
In order for the LocationResponder to find the correct route helper for namespaced routes you need to pass the namespaces to `respond_with`:
|
90
109
|
|
91
|
-
|
92
|
-
|
93
|
-
|
110
|
+
```ruby
|
111
|
+
class Api::V1::ThingsController < ApplicationController
|
112
|
+
respond_to :json
|
94
113
|
|
95
|
-
|
96
|
-
|
114
|
+
# POST /api/v1/things
|
115
|
+
def create
|
116
|
+
@thing = Thing.create(thing_params)
|
117
|
+
respond_with :api, :v1, @thing
|
118
|
+
end
|
119
|
+
end
|
97
120
|
```
|
98
121
|
|
99
|
-
|
100
|
-
|
101
|
-
```ruby
|
102
|
-
gem 'responders'
|
103
|
-
```
|
122
|
+
## Configuring your own responder
|
104
123
|
|
105
|
-
Responders only provides a set of modules
|
106
|
-
responder.
|
124
|
+
Responders only provides a set of modules and to use them you have to create your own
|
125
|
+
responder. After you run the install command, the following responder will be
|
126
|
+
generated in your application:
|
107
127
|
|
108
128
|
```ruby
|
109
|
-
# lib/
|
110
|
-
class
|
129
|
+
# lib/application_responder.rb
|
130
|
+
class ApplicationResponder < ActionController::Responder
|
111
131
|
include Responders::FlashResponder
|
112
132
|
include Responders::HttpCacheResponder
|
113
133
|
end
|
114
134
|
```
|
115
135
|
|
116
|
-
|
136
|
+
Your application also needs to be configured to use it:
|
117
137
|
|
118
138
|
```ruby
|
119
139
|
# app/controllers/application_controller.rb
|
120
|
-
require "
|
140
|
+
require "application_responder"
|
121
141
|
|
122
142
|
class ApplicationController < ActionController::Base
|
123
|
-
self.responder =
|
143
|
+
self.responder = ApplicationResponder
|
124
144
|
respond_to :html
|
125
145
|
end
|
126
146
|
```
|
127
147
|
|
128
|
-
Or, for your convenience, just do:
|
129
|
-
|
130
|
-
```console
|
131
|
-
rails generate responders:install
|
132
|
-
```
|
133
|
-
|
134
148
|
## Controller method
|
135
149
|
|
136
150
|
This gem also includes the controller method `responders`, which allows you to cherry-pick which
|
@@ -144,7 +158,7 @@ end
|
|
144
158
|
|
145
159
|
## Interpolation Options
|
146
160
|
|
147
|
-
You can pass in extra interpolation options for the translation by adding an `
|
161
|
+
You can pass in extra interpolation options for the translation by adding an `flash_interpolation_options` method to your controller:
|
148
162
|
|
149
163
|
```ruby
|
150
164
|
class InvitationsController < ApplicationController
|
@@ -157,33 +171,120 @@ class InvitationsController < ApplicationController
|
|
157
171
|
|
158
172
|
private
|
159
173
|
|
160
|
-
def
|
174
|
+
def flash_interpolation_options
|
161
175
|
{ resource_name: @invitation.email }
|
162
176
|
end
|
163
177
|
end
|
164
178
|
```
|
165
179
|
|
166
|
-
Now you would see the message "
|
180
|
+
Now you would see the message `"name@example.com was successfully created"` instead of the default `"Invitation was successfully created."`
|
167
181
|
|
168
182
|
## Generator
|
169
183
|
|
170
184
|
This gem also includes a responders controller generator, so your scaffold can be customized
|
171
|
-
to use `respond_with` instead of default `respond_to` blocks.
|
172
|
-
|
185
|
+
to use `respond_with` instead of default `respond_to` blocks. From 2.1, you need to explicitly opt-in to use this generator by adding the following to your `config/application.rb`:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
config.app_generators.scaffold_controller :responders_controller
|
189
|
+
```
|
190
|
+
|
191
|
+
## Failure handling
|
192
|
+
|
193
|
+
Responders don't use `valid?` to check for errors in models to figure out if
|
194
|
+
the request was successful or not, and relies on your controllers to call
|
195
|
+
`save` or `create` to trigger the validations.
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
def create
|
199
|
+
@widget = Widget.new(widget_params)
|
200
|
+
# @widget will be a valid record for responders, as we haven't called `save`
|
201
|
+
# on it, and will always redirect to the `widgets_path`.
|
202
|
+
respond_with @widget, location: -> { widgets_path }
|
203
|
+
end
|
204
|
+
```
|
205
|
+
|
206
|
+
Responders will check if the `errors` object in your model is empty or not. Take
|
207
|
+
this in consideration when implementing different actions or writing test
|
208
|
+
assertions on this behavior for your controllers.
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
def create
|
212
|
+
@widget = Widget.new(widget_params)
|
213
|
+
@widget.errors.add(:base, :invalid)
|
214
|
+
# `respond_with` will render the `new` template again,
|
215
|
+
# and set the status based on the configured `error_status`.
|
216
|
+
respond_with @widget
|
217
|
+
end
|
218
|
+
```
|
219
|
+
|
220
|
+
## Verifying request formats
|
221
|
+
|
222
|
+
`respond_with` will raise an `ActionController::UnknownFormat` if the request
|
223
|
+
MIME type was not configured through the class level `respond_to`, but the
|
224
|
+
action will still be executed and any side effects (like creating a new record)
|
225
|
+
will still occur. To raise the `UnknownFormat` exception before your action
|
226
|
+
is invoked you can set the `verify_requested_format!` method as a `before_action`
|
227
|
+
on your controller.
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
class WidgetsController < ApplicationController
|
231
|
+
respond_to :json
|
232
|
+
before_action :verify_requested_format!
|
233
|
+
|
234
|
+
# POST /widgets.html won't reach the `create` action.
|
235
|
+
def create
|
236
|
+
widget = Widget.create(widget_params)
|
237
|
+
respond_with widget
|
238
|
+
end
|
239
|
+
end
|
240
|
+
```
|
241
|
+
|
242
|
+
## Configuring error and redirect statuses
|
243
|
+
|
244
|
+
By default, `respond_with` will respond to errors on `HTML` & `JS` requests using the HTTP status code `200 OK`,
|
245
|
+
and perform redirects using the HTTP status code `302 Found`, both for backwards compatibility reasons.
|
246
|
+
|
247
|
+
You can configure this behavior by setting `config.responders.error_status` and `config.responders.redirect_status` to the desired status codes.
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
config.responders.error_status = :unprocessable_entity
|
251
|
+
config.responders.redirect_status = :see_other
|
252
|
+
```
|
253
|
+
|
254
|
+
These can also be set in your custom `ApplicationResponder` if you have generated one: (see install instructions)
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
class ApplicationResponder < ActionController::Responder
|
258
|
+
self.error_status = :unprocessable_entity
|
259
|
+
self.redirect_status = :see_other
|
260
|
+
end
|
261
|
+
```
|
262
|
+
|
263
|
+
_Note_: the application responder generated for new apps already configures a different set of defaults: `422 Unprocessable Entity` for errors, and `303 See Other` for redirects. _Responders may change the defaults to match these in a future major release._
|
264
|
+
|
265
|
+
### Hotwire/Turbo and fetch APIs
|
266
|
+
|
267
|
+
Hotwire/Turbo expects successful redirects after form submissions to respond with HTTP status `303 See Other`, and error responses to be 4xx or 5xx statuses, for example `422 Unprocessable Entity` for displaying form validation errors and `500 Internal Server Error` for other server errors. [Turbo documentation: Redirecting After a Form Submission](https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission).
|
268
|
+
|
269
|
+
The example configuration showed above matches the statuses that better integrate with Hotwire/Turbo.
|
173
270
|
|
174
271
|
## Examples
|
175
272
|
|
176
|
-
Want more examples ? Check out
|
273
|
+
Want more examples ? Check out these blog posts:
|
177
274
|
|
178
|
-
* [One in Three: Inherited Resources, Has Scope and Responders](http://blog.plataformatec.com.br/2009/12/one-in-three-inherited-resources-has-scope-and-responders/)
|
179
275
|
* [Embracing REST with mind, body and soul](http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/)
|
180
276
|
* [Three reasons to love ActionController::Responder](http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder/)
|
181
|
-
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3
|
277
|
+
* [My five favorite things about Rails 3](http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3)
|
278
|
+
|
279
|
+
## Supported Ruby / Rails versions
|
280
|
+
|
281
|
+
We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.
|
282
|
+
|
283
|
+
For more information about specific versions please check [Ruby](https://www.ruby-lang.org/en/downloads/branches/)
|
284
|
+
and [Rails](https://guides.rubyonrails.org/maintenance_policy.html) maintenance policies, and our test matrix.
|
182
285
|
|
183
286
|
## Bugs and Feedback
|
184
287
|
|
185
288
|
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
186
289
|
|
187
|
-
|
188
|
-
|
189
|
-
MIT License. Copyright 2009-2014 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
290
|
+
MIT License. Copyright 2020-2023 Rafael França, Carlos Antônio da Silva. Copyright 2009-2019 Plataformatec.
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
require 'action_controller/metal/mime_responds'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
3
|
+
require "active_support/core_ext/array/extract_options"
|
4
|
+
require "action_controller/metal/mime_responds"
|
5
|
+
|
6
|
+
module ActionController # :nodoc:
|
5
7
|
module RespondWith
|
6
8
|
extend ActiveSupport::Concern
|
7
9
|
|
@@ -37,17 +39,17 @@ module ActionController #:nodoc:
|
|
37
39
|
def respond_to(*mimes)
|
38
40
|
options = mimes.extract_options!
|
39
41
|
|
40
|
-
only_actions = Array(options.delete(:only)).map(&:
|
41
|
-
except_actions = Array(options.delete(:except)).map(&:
|
42
|
+
only_actions = Array(options.delete(:only)).map(&:to_sym)
|
43
|
+
except_actions = Array(options.delete(:except)).map(&:to_sym)
|
42
44
|
|
43
|
-
|
45
|
+
hash = mimes_for_respond_to.dup
|
44
46
|
mimes.each do |mime|
|
45
47
|
mime = mime.to_sym
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
hash[mime] = {}
|
49
|
+
hash[mime][:only] = only_actions unless only_actions.empty?
|
50
|
+
hash[mime][:except] = except_actions unless except_actions.empty?
|
49
51
|
end
|
50
|
-
self.mimes_for_respond_to =
|
52
|
+
self.mimes_for_respond_to = hash.freeze
|
51
53
|
end
|
52
54
|
|
53
55
|
# Clear all mime types in <tt>respond_to</tt>.
|
@@ -93,7 +95,10 @@ module ActionController #:nodoc:
|
|
93
95
|
# i.e. its +show+ action.
|
94
96
|
# 2. If there are validation errors, the response
|
95
97
|
# renders a default action, which is <tt>:new</tt> for a
|
96
|
-
# +post+ request or <tt>:edit</tt> for +patch+ or +put
|
98
|
+
# +post+ request or <tt>:edit</tt> for +patch+ or +put+,
|
99
|
+
# and the status is set based on the configured `error_status`.
|
100
|
+
# (defaults to `422 Unprocessable Entity` on new apps,
|
101
|
+
# `200 OK` for compatibility reasons on old apps.)
|
97
102
|
# Thus an example like this -
|
98
103
|
#
|
99
104
|
# respond_to :html, :xml
|
@@ -114,8 +119,8 @@ module ActionController #:nodoc:
|
|
114
119
|
# format.html { redirect_to(@user) }
|
115
120
|
# format.xml { render xml: @user }
|
116
121
|
# else
|
117
|
-
# format.html { render action: "new" }
|
118
|
-
# format.xml { render xml: @user }
|
122
|
+
# format.html { render action: "new", status: :unprocessable_entity }
|
123
|
+
# format.xml { render xml: @user, status: :unprocessable_entity }
|
119
124
|
# end
|
120
125
|
# end
|
121
126
|
# end
|
@@ -175,26 +180,34 @@ module ActionController #:nodoc:
|
|
175
180
|
# Also, a hash passed to +respond_with+ immediately after the specified
|
176
181
|
# resource(s) is interpreted as a set of options relevant to all
|
177
182
|
# formats. Any option accepted by +render+ can be used, e.g.
|
183
|
+
#
|
178
184
|
# respond_with @people, status: 200
|
185
|
+
#
|
179
186
|
# However, note that these options are ignored after an unsuccessful attempt
|
180
187
|
# to save a resource, e.g. when automatically rendering <tt>:new</tt>
|
181
188
|
# after a post request.
|
182
189
|
#
|
183
|
-
#
|
190
|
+
# Three additional options are relevant specifically to +respond_with+ -
|
184
191
|
# 1. <tt>:location</tt> - overwrites the default redirect location used after
|
185
192
|
# a successful html +post+ request.
|
186
193
|
# 2. <tt>:action</tt> - overwrites the default render action used after an
|
187
194
|
# unsuccessful html +post+ request.
|
195
|
+
# 3. <tt>:render</tt> - allows to pass any options directly to the <tt>:render<tt/>
|
196
|
+
# call after unsuccessful html +post+ request. Useful if for example you
|
197
|
+
# need to render a template which is outside of controller's path or you
|
198
|
+
# want to override the default http <tt>:status</tt> code, e.g.
|
199
|
+
#
|
200
|
+
# respond_with(resource, render: { template: 'path/to/template', status: 418 })
|
188
201
|
def respond_with(*resources, &block)
|
189
202
|
if self.class.mimes_for_respond_to.empty?
|
190
203
|
raise "In order to use respond_with, first you need to declare the " \
|
191
204
|
"formats your controller responds to in the class level."
|
192
205
|
end
|
193
206
|
|
194
|
-
mimes = collect_mimes_from_class_level
|
207
|
+
mimes = collect_mimes_from_class_level
|
195
208
|
collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
|
196
209
|
block.call(collector) if block_given?
|
197
|
-
|
210
|
+
|
198
211
|
if format = collector.negotiate_format(request)
|
199
212
|
_process_format(format)
|
200
213
|
options = resources.size == 1 ? {} : resources.extract_options!
|
@@ -206,12 +219,31 @@ module ActionController #:nodoc:
|
|
206
219
|
end
|
207
220
|
end
|
208
221
|
|
209
|
-
|
222
|
+
protected
|
223
|
+
|
224
|
+
# Before action callback that can be used to prevent requests that do not
|
225
|
+
# match the mime types defined through <tt>respond_to</tt> from being executed.
|
226
|
+
#
|
227
|
+
# class PeopleController < ApplicationController
|
228
|
+
# respond_to :html, :xml, :json
|
229
|
+
#
|
230
|
+
# before_action :verify_requested_format!
|
231
|
+
# end
|
232
|
+
def verify_requested_format!
|
233
|
+
mimes = collect_mimes_from_class_level
|
234
|
+
collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
|
235
|
+
|
236
|
+
unless collector.negotiate_format(request)
|
237
|
+
raise ActionController::UnknownFormat
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
alias :verify_request_format! :verify_requested_format!
|
210
242
|
|
211
243
|
# Collect mimes declared in the class method respond_to valid for the
|
212
244
|
# current action.
|
213
|
-
def collect_mimes_from_class_level
|
214
|
-
action = action_name.
|
245
|
+
def collect_mimes_from_class_level # :nodoc:
|
246
|
+
action = action_name.to_sym
|
215
247
|
|
216
248
|
self.class.mimes_for_respond_to.keys.select do |mime|
|
217
249
|
config = self.class.mimes_for_respond_to[mime]
|
@@ -226,4 +258,4 @@ module ActionController #:nodoc:
|
|
226
258
|
end
|
227
259
|
end
|
228
260
|
end
|
229
|
-
end
|
261
|
+
end
|