A summary of data about the Ruby ecosystem.

https://github.com/heartcombo/responders

A set of Rails responders to dry up your application
https://github.com/heartcombo/responders

Keywords

controllers flash-messages rails ruby

Keywords from Contributors

activerecord activejob mvc devise rubygems rack rspec dsl form-builder rails-helper

Last synced: about 2 hours ago
JSON representation

Repository metadata

A set of Rails responders to dry up your application

README.md

Responders

Gem Version

A set of responders modules to dry up your Rails app.

Installation

Add the responders gem to your Gemfile:

gem "responders"

Update your bundle and run the install generator:

$ bundle install
$ rails g responders:install

If you are including this gem to support backwards compatibility for responders in previous releases of Rails, you only need to include the gem and bundle.

$ bundle install

Responders Types

FlashResponder

Sets the flash based on the controller action and resource status.
For instance, if you do: respond_with(@post) on a POST request and the resource @post
does not contain errors, it will automatically set the flash message to
"Post was successfully created" as long as you configure your I18n file:

  flash:
    actions:
      create:
        notice: "%{resource_name} was successfully created."
      update:
        notice: "%{resource_name} was successfully updated."
      destroy:
        notice: "%{resource_name} was successfully destroyed."
        alert: "%{resource_name} could not be destroyed."

In case the resource contains errors, you should use the failure key on I18n. This is
useful to dry up flash messages from your controllers. Note: by default alerts for update
and destroy actions are commented in generated I18n file. If you need a specific message
for a controller, let's say, for PostsController, you can also do:

  flash:
    posts:
      create:
        notice: "Your post was created and will be published soon"

This responder is activated in all non get requests. By default it will use the keys
:notice and :alert, but they can be changed in your application:

config.responders.flash_keys = [ :success, :failure ]

You can also have embedded HTML. Just create a _html scope.

  flash:
    actions:
      create:
        alert_html: "<strong>OH NOES!</strong> You did it wrong!"
    posts:
      create:
        notice_html: "<strong>Yay!</strong> You did it!"

See also the namespace_lookup option to search the full hierarchy of possible keys.

HttpCacheResponder

Automatically adds Last-Modified headers to API requests. This
allows clients to easily query the server if a resource changed and if the client tries
to retrieve a resource that has not been modified, it returns not_modified status.

CollectionResponder

Makes your create and update action redirect to the collection on success.

LocationResponder

This responder allows you to use callable objects as the redirect location.
Useful when you want to use the respond_with method with
a custom route that requires persisted objects, but the validation may fail.

Note: this responder is included by default, and doesn't need to be included
on the top of your controller (including it will issue a deprecation warning).

class ThingsController < ApplicationController
  respond_to :html

  def create
    @thing = Thing.create(params[:thing])
    respond_with @thing, location: -> { thing_path(@thing) }
  end
end

Dealing with namespaced routes

In order for the LocationResponder to find the correct route helper for namespaced routes you need to pass the namespaces to respond_with:

class Api::V1::ThingsController < ApplicationController
  respond_to :json

  # POST /api/v1/things
  def create
    @thing = Thing.create(thing_params)
    respond_with :api, :v1, @thing
  end
end

Configuring your own responder

Responders only provides a set of modules and to use them you have to create your own
responder. After you run the install command, the following responder will be
generated in your application:

# lib/application_responder.rb
class ApplicationResponder < ActionController::Responder
  include Responders::FlashResponder
  include Responders::HttpCacheResponder
end

Your application also needs to be configured to use it:

# app/controllers/application_controller.rb
require "application_responder"

class ApplicationController < ActionController::Base
  self.responder = ApplicationResponder
  respond_to :html
end

Controller method

This gem also includes the controller method responders, which allows you to cherry-pick which
responders you want included in your controller.

class InvitationsController < ApplicationController
  responders :flash, :http_cache
end

Interpolation Options

You can pass in extra interpolation options for the translation by adding an flash_interpolation_options method to your controller:

class InvitationsController < ApplicationController
  responders :flash, :http_cache

  def create
    @invitation = Invitation.create(params[:invitation])
    respond_with @invitation
  end

  private

  def flash_interpolation_options
    { resource_name: @invitation.email }
  end
end

Now you would see the message "name@example.com was successfully created" instead of the default "Invitation was successfully created."

Generator

This gem also includes a responders controller generator, so your scaffold can be customized
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:

config.app_generators.scaffold_controller :responders_controller

Failure handling

Responders don't use valid? to check for errors in models to figure out if
the request was successful or not, and relies on your controllers to call
save or create to trigger the validations.

def create
  @widget = Widget.new(widget_params)
  # @widget will be a valid record for responders, as we haven't called `save`
  # on it, and will always redirect to the `widgets_path`.
  respond_with @widget, location: -> { widgets_path }
end

Responders will check if the errors object in your model is empty or not. Take
this in consideration when implementing different actions or writing test
assertions on this behavior for your controllers.

def create
  @widget = Widget.new(widget_params)
  @widget.errors.add(:base, :invalid)
  # `respond_with` will render the `new` template again,
  # and set the status based on the configured `error_status`.
  respond_with @widget
end

Verifying request formats

respond_with will raise an ActionController::UnknownFormat if the request
MIME type was not configured through the class level respond_to, but the
action will still be executed and any side effects (like creating a new record)
will still occur. To raise the UnknownFormat exception before your action
is invoked you can set the verify_requested_format! method as a before_action
on your controller.

class WidgetsController < ApplicationController
  respond_to :json
  before_action :verify_requested_format!

  # POST /widgets.html won't reach the `create` action.
  def create
    widget = Widget.create(widget_params)
    respond_with widget
  end
end

Configuring error and redirect statuses

By default, respond_with will respond to errors on HTML & JS requests using the HTTP status code 200 OK,
and perform redirects using the HTTP status code 302 Found, both for backwards compatibility reasons.

You can configure this behavior by setting config.responders.error_status and config.responders.redirect_status to the desired status codes.

config.responders.error_status = :unprocessable_entity
config.responders.redirect_status = :see_other

These can also be set in your custom ApplicationResponder if you have generated one: (see install instructions)

class ApplicationResponder < ActionController::Responder
  self.error_status = :unprocessable_entity
  self.redirect_status = :see_other
end

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.

Hotwire/Turbo and fetch APIs

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.

The example configuration showed above matches the statuses that better integrate with Hotwire/Turbo.

Examples

Want more examples ? Check out these blog posts:

Supported Ruby / Rails versions

We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.

For more information about specific versions please check Ruby
and Rails maintenance policies, and our test matrix.

Bugs and Feedback

If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.

License

MIT License.
Copyright 2020-2025 Rafael França, Carlos Antonio da Silva.
Copyright 2009-2019 Plataformatec.


Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 8 days ago

Total Commits: 363
Total Committers: 74
Avg Commits per committer: 4.905
Development Distribution Score (DDS): 0.763

Commits in past year: 6
Committers in past year: 1
Avg Commits per committer in past year: 6.0
Development Distribution Score (DDS) in past year: 0.0

Name Email Commits
Carlos Antonio da Silva c****a@g****m 86
José Valim j****m@g****m 75
Rafael Mendonça França r****a@g****m 29
Rafael Mendonça França r****a@p****r 27
Lucas Mazza l****a@p****r 19
Yury Velikanau y****u@g****m 14
José Valim j****m@p****r 11
Nando Vieira f****a@g****m 7
David Rodríguez d****z@r****t 6
Vasiliy Ermolovich y****h@g****m 5
Marc-Andre Lafortune g****b@m****a 4
Matthew Rudy Jacobs m****s@g****m 4
Yuri S f****i@g****m 3
Marcos Ferreira m****f@g****m 3
Iain Hecker i****n@i****l 3
mikhail-alhimik m****k@g****m 2
Marcelo G. Cajueiro m****o@g****m 2
Leonardo Tegon l****3@g****m 2
Jean Boussier j****r@g****m 2
Deepak N e****3@g****m 2
Chris Oliver e****3@g****m 2
Erich Kist e****t@p****r 2
Igor Kapkov i****k@m****m 2
Paulo Schiavon p****o@l****m 1
Akansha Kumari a****4@g****m 1
Akira Matsuda r****e@d****p 1
Atul Bhosale a****e@g****m 1
Damir Svrtan d****n@g****m 1
David Van Der Beek e****k@g****m 1
Edouard CHIN e****n@s****m 1
and 44 more...

Committer domains:


Issue and Pull Request metadata

Last synced: about 2 months ago

Total issues: 59
Total pull requests: 48
Average time to close issues: 11 months
Average time to close pull requests: 2 months
Total issue authors: 55
Total pull request authors: 32
Average comments per issue: 3.25
Average comments per pull request: 2.15
Merged pull request: 34
Bot issues: 0
Bot pull requests: 0

Past year issues: 0
Past year pull requests: 2
Past year average time to close issues: N/A
Past year average time to close pull requests: 11 minutes
Past year issue authors: 0
Past year pull request authors: 2
Past year average comments per issue: 0
Past year average comments per pull request: 0.5
Past year merged pull request: 1
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/heartcombo/responders

Top Issue Authors

  • amilligan (2)
  • phsacramento (2)
  • arbesulo (2)
  • JakeTheSnake3p0 (2)
  • itkin (1)
  • svevang (1)
  • ashjambhulkar (1)
  • igorkasyanchuk (1)
  • patrick99e99 (1)
  • boardfish (1)
  • barberj (1)
  • ericgascoine (1)
  • chabgood (1)
  • d3crypt3d (1)
  • Edouard-chin (1)

Top Pull Request Authors

  • deivid-rodriguez (7)
  • carlosantoniodasilva (3)
  • lucasmazza (3)
  • mracos (2)
  • kzink (2)
  • excid3 (2)
  • kianmeng (2)
  • casperisfine (2)
  • Edouard-chin (2)
  • tegon (1)
  • frausto (1)
  • zzak (1)
  • Fudoshiki (1)
  • joelzwarrington (1)
  • oystersauce8 (1)

Top Issue Labels

  • Needs more info (7)
  • Discussion (2)

Top Pull Request Labels


Package metadata

gem.coop: responders

A set of Rails responders to dry up your application

  • Homepage: https://github.com/heartcombo/responders
  • Documentation: http://www.rubydoc.info/gems/responders/
  • Licenses: MIT
  • Latest release: 3.2.0 (published 2 months ago)
  • Last Synced: 2025-12-08T12:30:28.545Z (4 days ago)
  • Versions: 49
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 295,110,164 Total
  • Docker Downloads: 529,338,135
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 0.067%
    • Downloads: 0.082%
    • Docker downloads count: 0.188%
  • Maintainers (2)
rubygems.org: responders

A set of Rails responders to dry up your application

  • Homepage: https://github.com/heartcombo/responders
  • Documentation: http://www.rubydoc.info/gems/responders/
  • Licenses: MIT
  • Latest release: 3.2.0 (published 2 months ago)
  • Last Synced: 2025-12-08T21:33:54.533Z (4 days ago)
  • Versions: 49
  • Dependent Packages: 195
  • Dependent Repositories: 182,823
  • Downloads: 295,186,072 Total
  • Docker Downloads: 529,338,135
  • Rankings:
    • Downloads: 0.081%
    • Dependent repos count: 0.097%
    • Dependent packages count: 0.192%
    • Docker downloads count: 0.23%
    • Average: 0.633%
    • Stargazers count: 1.118%
    • Forks count: 2.078%
  • Maintainers (2)
proxy.golang.org: github.com/heartcombo/responders

  • Homepage:
  • Documentation: https://pkg.go.dev/github.com/heartcombo/responders#section-documentation
  • Licenses: mit
  • Latest release: v3.2.0+incompatible (published 2 months ago)
  • Last Synced: 2025-12-07T18:02:48.456Z (5 days ago)
  • Versions: 43
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent packages count: 6.497%
    • Average: 6.716%
    • Dependent repos count: 6.936%

Dependencies

.github/workflows/rubocop.yml actions
  • actions/checkout v3 composite
  • ruby/setup-ruby v1 composite
.github/workflows/test.yml actions
  • actions/checkout v3 composite
  • ruby/setup-ruby v1 composite
Gemfile rubygems
  • activemodel ~> 7.0.0
  • mocha >= 0
  • rails-controller-testing >= 0
  • railties ~> 7.0.0
  • rubocop >= 0
  • rubocop-performance >= 0
Gemfile.lock rubygems
  • actionpack 7.0.4
  • actionview 7.0.4
  • activemodel 7.0.4
  • activesupport 7.0.4
  • ast 2.4.2
  • builder 3.2.4
  • bundler 2.4.5
  • concurrent-ruby 1.1.10
  • crass 1.0.6
  • erubi 1.12.0
  • i18n 1.12.0
  • json 2.6.3
  • loofah 2.19.1
  • method_source 1.0.0
  • mini_portile2 2.8.1
  • minitest 5.17.0
  • mocha 2.0.2
  • nokogiri 1.14.0
  • parallel 1.22.1
  • parser 3.2.0.0
  • racc 1.6.2
  • rack 2.2.6
  • rack-test 2.0.2
  • rails-controller-testing 1.0.5
  • rails-dom-testing 2.0.3
  • rails-html-sanitizer 1.4.4
  • railties 7.0.4
  • rainbow 3.1.1
  • rake 13.0.6
  • regexp_parser 2.6.1
  • responders 3.1.0
  • rexml 3.2.5
  • rubocop 1.43.0
  • rubocop-ast 1.24.1
  • rubocop-performance 1.15.2
  • ruby-progressbar 1.11.0
  • ruby2_keywords 0.0.5
  • thor 1.2.1
  • tzinfo 2.0.5
  • unicode-display_width 2.4.2
  • zeitwerk 2.6.6
responders.gemspec rubygems
  • actionpack >= 5.2
  • railties >= 5.2

Score: 33.15757071201948