4

I am building a Rails api and currently have this folder structure:

enter image description here

The error_serializer.rb file is a module:

module ErrorSerializer
  extend ActiveSupport::Concern

  ...methods here...
end

Which I can include in any of the api controllers, for example:

class Api::TemplatesController < ApiController
  include ErrorSerializer
  ...
end

But since this errors_serializer module is only relevant to api controllers, I want to move the file to 'api/concerns/error_serializer.rb'.

But that generates the error:

ActionController::RoutingError (uninitialized constant Api::TemplatesController::ErrorSerializer)

I tried changing the name inside the file to:

module Api::ErrorSerialzer

but got the same error.

So what must I change to be able to move that file?

2
  • Did you change the include to be Api::ErrorSerializer? Commented Oct 7, 2017 at 16:26
  • @AndrewMarshall Yes I did. Commented Oct 7, 2017 at 16:34

2 Answers 2

12

Since rails expects your module naming to follow your file structure, your concern should be named:

module Api::Concerns::ErrorSerializer

Since you're including it in Api::TemplatesController, I would do:

class Api::TemplatesController < ApiController
  include Api::Concerns::ErrorSerializer
  ...
end

To help rails out with the constant lookup.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to the answer from @jvillian and this blog post, I was able to figure out the 'Rails' way to do this (since actually I will need the concern in all Api controllers, and also my api controller was outside the api namespace). So I'm posting this solution as (I think) it's the preferred way:

I moved the error_serialzier.rb file into api/concerns and change the code to include the Api namespace:

module Api::Concerns::ErrorSerializer
  extend ActiveSupport::Concern
  ...
end

I also moved api_controller.rb file and put it inside the /api folder, and thus into the API module namespace, so now it looks like this:

class Api::ApiController < ActionController::API
  before_action :authenticate_api_user!
  include DeviseTokenAuth::Concerns::SetUserByToken 
  include Concerns::ErrorSerializer

  respond_to :json
end

This got rid of the uninitialized constant errors.

4 Comments

Do you know how to include error_serializer.rb if error_serializer.rb file is not in /api/concerns, but still in /concerns?
@ToshihiroYokota I don't work in Rails any more, but try include Concerns::ErrorSerializer
@rmcharry It just worked. I understand it works if I recognize "rails expects your module naming to follow your file structure". Thanks for that.
@ToshihiroYokota Good to hear it worked, thanks for letting me know! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.