C404
C404

Reputation: 243

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!

Upvotes: 7

Views: 3337

Answers (2)

Vees
Vees

Reputation: 713

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

Upvotes: 19

jottr
jottr

Reputation: 3293

It seems that you misnamed your jbuilder view.
It should be app/views/users/show.json.jbuilder instead of app/views/users/show.json.builder.

Upvotes: -1

Related Questions