Making jbuilder work with Rails 5.0.0.beta1.1 in API mode
I'm using Rails 5.0.0.beta1.1 in API mode. I've been unable to make jbuilder to work with Rails.
I've got the following code:
# app/controllers/tests_controller.rb
class TestsController < ApplicationController
# The requests gets inside the action
def test
end
end
# app/views/tests/test.json.jbuilder
json.test "It works!"
I'm getting this error when I make a request to tests#test:
No template found for TestsController#test, rendering head :no_content
I guess I have to change some things in the config files. What do I have to do?
It was an error on my end, nothing to do with jbuilder at all. Please, close this issue. Sorry for the inconvinience
You have the power to close your own issues by the way. Glad it's been resolved for you. :)
@sauronnikko how did you fix it? I'm running into the same problem.
I figured it out why my rails app (API only mode) was having this problem.
Heres my spec/requests/users_spec.rb (which was generated by rails g scafford)
require 'rails_helper'
RSpec.describe "Users", :type => :request do
describe "GET /users" do
it "works! (now write some real specs)" do
get users_path
expect(response).to have_http_status(200)
end
end
end
My test.log file was outputting...
Started GET "/users" for 127.0.0.1 at 2016-02-03 18:22:41 -0500
Processing by UsersController#index as HTML
No template found for UsersController#index, rendering head :no_content
Completed 204 No Content in 2ms (ActiveRecord: 0.0ms)
The problem is that users_path generates /users instead of /users.json. So because it was hitting /users, it was looking for an HTML template (which i dont have because Im API only).
So to fix it I converted users_path -> users_path(format: :json) and that fixed it.
Having similar issue with 5.0.0.beta2
class PostsController < ApplicationController
def first
render json: {error: 'This is Error'}
end
def second
@data = [1,2,3]
render json: {error: 'This is Error'}
end
end
app/views/posts/second.json.jbuilder
json.key @data
(Have not created template for 'first' action)
http://localhost:3000/first.json
{
status: 500,
error: "Internal Server Error",
exception: "#<ActionView::MissingTemplate: Missing template posts/first, application/first with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: * "/Users/a/workspace/new_api/app/views" >"
}
http://localhost:3000/second.json
{
key: [1,2,3]
}
The issue is it never considers the options(specifically :json) passed to render call in controller action and always tries to render the view template for the action.
I think the issue is with method 'render_to_body' of ActionView::Rendering and ActionController::Renderers
Since ActionController::API does not include ActionView::Layouts and jbuilder includes it specifically, somehow it executes render_to_body from ActionView::Rendering before ActionController::Renderers and hence always renders template irrespective of ':json' option specified in render call from controller.
Thoughts?
@CBarraford I was specifying an API version in the Accept header for an API which did not have versions (my request was copy-pasted and I forgot to remove the header). Once I removed the header, jbuilder worked.
However, I do present the same issue as @abhi-patel
Doing an explicit render from the controller like this works:
render 'controller_name/action.json.jbuilder'
+1 the same issue as @abhi-patel
+1 also same issue as @abhi-patel
Looks like you're hitting https://github.com/rails/rails/pull/23923, fixed by https://github.com/rails/rails/pull/24178 and https://github.com/rails/jbuilder/pull/327.
Seems still broken here.
Doing render json: @post, status: :created
results in ActionView::MissingTemplate (Missing template posts/create....
On the other hand, the workaround render plain: @post.to_json, content_type: 'application/json' succeeds.
API:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :destroy]
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
render json: @post, status: :created
end
end
Gemfile:
gem 'rails', '5.0.0'
gem 'puma', '3.4.0'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks', '5.0.0'
gem 'jbuilder', '2.4.1'
gem 'pg', '0.18.4'
gem 'rack-cors', :require => 'rack/cors'
group :development, :test do
gem 'byebug', '9.0.0', platform: :mri
end
group :development do
gem 'web-console', '3.1.1'
gem 'listen', '3.0.8'
gem 'spring', '1.7.2'
gem 'spring-watcher-listen', '2.0.0'
gem 'awesome_print', :require => 'ap'
end
group :test do
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
In my case I didn't have the jbuilder gem installed and was getting this error.