Jbuilder does not work with gem rails-api

I am trying to use rails-api gem with jbuilder and I cannot get it to work.

Here is an example of my rails-api / jbuilder controller views

Gemfile

gem 'jbuilder' 

Application / controller 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 Jbuilder's documentation, this should work fine, but still nothing is returned.

Thanks for the help!

+6
source share
2 answers

I know that the original problem was probably resolved. However, I had the same problem and finally I found the answer.

Since the rails-api version of the rails has been truncated, some functions are missing (in this case, implicit rendaring representations), and we need to explicitly add them.

The easiest solution is to add an ActionController :: ImplicitRender to the ApplicationController

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

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

0
source

Source: https://habr.com/ru/post/975616/


All Articles