Is it possible to include Rails actionpack in a separate require statement?

786 Views Asked by At

I have an API controller that does not inherit from Application controller. I'm trying to include the respond_to method and get a method undefined error....So then I required actionpack at the top like below:

require 'actionpack'

class Api::V1::UsersController < Api::ApiController
  version 1

  doorkeeper_for :all
  respond_to :json

  def show
    respond_with current_user.as_json(except: :password_digest)
  end
end

and I got the following:

LoadError (cannot load such file -- actionpack): app/controllers/api/v1/users_controller.rb:1:in `'

I already have gem 'actionpack' in my Gemfile as well as Rails 4.1.2 (this is part of a Rails 4 application)

2

There are 2 best solutions below

0
On

bah, I just realized I didn't add the underscore as in:

require 'action_pack'
0
On

You want to mixin an action_pack module into your ApiController. For this you need include not require. If you're in Rails action_pack is already loaded, if not and you've bundled the action_pack gem then yea require it. Then you need to:

class Api::V1::UsersController < Api::ApiController
  include ActionController::MimeResponds

The ActionController::MimeResponds module will be mixed into your controller and now the respond_to method will be available to it.

Read the source, Luke