Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 765
C404 Avatar asked Sep 21 '14 21:09

C404


1 Answers

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
like image 136
Vees Avatar answered Sep 28 '22 07:09

Vees