There was an error while loading. Please reload this page.
Just include the boxer gem in your Gemfile:
boxer
Gemfile
gem 'boxer'
Then run bundle install to install the gem.
bundle install
It's recommended to use a Rails initializer to configure Boxer, usually config/initializers/boxer.rb.
config/initializers/boxer.rb
# Make URL helpers available in all boxes Boxer.configure do |config| config.box_includes = [Rails.application.routes.url_helpers] end
Now you can use those URL generation methods inside of all your boxes without thinking twice:
Boxer.box(:user) do |box, user| { :name => user.name, :url => user_path(user), } end
# Load all of our boxes unless Rails.env.test? Dir[File.join(Rails.root, 'lib', 'boxer', '**', '*.rb')].each do |f| require_dependency f end end
Using that code, you can create a Ruby file for each box inside lib/boxer/. So your User box could be defined in lib/boxer/user.rb.
lib/boxer/
lib/boxer/user.rb
In your API actions, simply render JSON from Boxer:
def index @users = Users.all.limit(10) render :json => @users.map { |u| Boxer.ship(:user, u) } end