Organize your controllers by grouping your before and after filters.
As developers, our teams are composed of a range of developers .... it's not always easy to put everybody in the same page
phases: authentication - your auth gem will do this one
pre-fetching-authorization fetching post-fetching-authorization
actual logic - action block
Our tests cases run with 1.9.3, 2.0.0 and 2.1.0
We encourage you to work with any 1.9+ Ruby version
You can use this gem with Rails 3.1, 3.2 and 4+
Add this line to your application's Gemfile:
gem 'before_actions'
And then execute:
bundle
If you want your scaffold generated code to always look like this, just run:
rails g before_actions:template
If you were already using this gem in your project and want to upgrade it, simply run:
bundle update before_actions
rails g before_actions:template
Then simply adjust your controllers to the new syntax
class ContactsController < ApplicationController
# load and authorize resources
before_actions do
# listing actions
only(:index) { @contacts = Contact.all }
# building actions
only(:new) { @contact = Contact.new }
only(:create) { @contact = Contact.new(contact_params) }
# member actions, will raise a 404 if the model is not found
only(:show, :edit, :update, :destroy) { @contact = Contact.find(params[:id]) }
end
after_actions do
all { your_code_here }
except(:index) { your_code_here }
end
around_actions do
only(:create) do |controller, action|
your_code
action.call
in_here
end
end
end
- Home page: https://github.com/before-actions-gem/before_actions
- Trello Board: https://trello.com/b/PMRfJAqq/before-actions-gem
- Bugs/Issues: https://github.com/before-actions-gem/before_actions/issues
- Fork it ( https://github.com/github.com/before-actions-gem/before_actions/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Before Actions uses Semantic Versioning 2.0.0