ruby-on-rails-4jbuilderrails-api

Jbuilder not working with gem rails-api


I'am trying rails-api gem with jbuilder and i can't seem to make it work

Here is a sample of my rails-api controller / jbuilder views

Gemfile

gem 'jbuilder'

Controller app/controller/users_controller.rb

 def show
  @user = User.find_by(id: params[:id])
 end

View app/views/users/show.json.builder

json.content format_content(@user.id)

According to the Jbuilder documentation this should work fine but still nothing is returned.

Thanks for the help!


Solution

  • I know that the original issue had probably been resolved. Nevertheless I had the same issue and finally found an answer.

    Since rails-api is stripped down version of rails some functionalities are missing (in this case implicit rendaring of views) and we need to add them explicitly.

    Simplest solution is to add ActionController::ImplicitRender to ApplicationController

    # app/controllers/application_controller.rb
    class ApplicationController < ActionController::API
      include ActionController::ImplicitRender
    end