BeforeActions <img src=“
” alt=“Gem Version” /> <img src=“
” alt=“Inline docs” /> <img src=“gemnasium.com/yakko/before_actions.svg” alt=“Dependency Status” />
BeforeActions an elegant way of loading resorces in your restful controllers.
Installation
In Rails 3 and Rails 4, add this to your Gemfile and run the bundle command.
gem "before_actions"
bundle
Instructions
1. Using the command
class ContactsController < ApplicationController
# load and authorize resources
before_actions do
# all actions
# actions { }
# list actions
actions(:index) { @contacts = Contact.all }
# building actions
actions(:new) { @contact = Contact.new }
actions(:create) { @contact = Contact.new(contact_params) }
# member actions, will raise a 404 if the model is not found
actions(:show, :edit, :update, :destroy) { @contact = Contact.find(params[:id]) }
# all actions
# actions { }
end
def contact_params
params.require(:contact).permit(:name)
end
2. Enjoy your clean controller
<img src=“readme_images/controller.jpg” alt=“image” />
3. Nested Routes
<img src=“readme_images/nested.png” alt=“image” />
License
Before Actions is released under the MIT License.