RailsLocationHeader
RailsLocationHeader is a Rails gem that automatically adds X-Entity-ID
and Location
headers when a new record is successfully created in a create
action. This helps in providing additional metadata about the created resource in API responses.
Installation
Add this gem to your application's Gemfile by executing:
bundle add rails_location_header
If bundler is not being used to manage dependencies, install the gem by executing:
gem install rails_location_header
Usage
To use this gem, include it in your Rails application. The gem automatically integrates with Rails controllers via a Railtie and works out of the box.
How It Works
-
create
action with a 201 status: Thecreate
action must return an HTTP201 Created
status. -
Instance variable: Declare an instance variable in the
create
action that matches the entity name (e.g.,@post
inPostsController
). -
show
action and route: Ensure the controller has ashow
action and a corresponding route to construct the resource's URL.
Headers Added
When the above requirements are met, the gem automatically adds the following headers to the response:
-
X-Entity-ID
: The ID of the created resource. -
Location
: The URL of the created resource.
No additional configuration is needed.
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run:
bundle exec rake install
To release a new version, update the version number in lib/rails_location_header/version.rb
, and then run:
bundle exec rake release
This will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/EliSebastian/rails_location_header. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the RailsLocationHeader project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.