Skip to content

Commit 5c130ef

Browse files
authored
Merge pull request #11 from akakiyo/update-render-to-body
Fix MissingTemplate error after Rails 8 upgrade
2 parents 215696d + bcdbbc7 commit 5c130ef

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/simple_json/simple_json_renderable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def render_to_body(options)
2929
# use super when any of [:body, :plain, :html] exist in options
3030
return super if self.class::RENDER_FORMATS_IN_PRIORITY.any? { |key| options.key? key }
3131

32-
template_name = template_name(options[:template] || action_name)
32+
template_name = template_name(options[:template] || options[:action] || action_name)
3333
if options.key?(:json)
3434
process_options(options)
3535
@rendered_format = 'application/json; charset=utf-8'

test/dummy_app/app/controllers/api/posts_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class PostsController < ActionController::API
88
def show(id)
99
@post = Post.find id
1010
end
11+
12+
def renamed_show(id)
13+
@post = Post.find id
14+
15+
render :show
16+
end
1117
end
1218
end
1319
end

test/dummy_app/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
if Rails::VERSION::MAJOR >= 5
2121
namespace :api do
2222
resources :posts, only: :show
23+
get '/renamed_posts/:id', to: 'posts#renamed_show'
2324
end
2425
end
2526
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

test/features/action_controller_api_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ class ActionControllerAPITest < ActionDispatch::IntegrationTest
1212

1313
assert_equal json, { 'title' => 'post 1' }
1414
end
15+
16+
test 'The renamed template correctly renders a JSON' do
17+
get '/api/renamed_posts/1.json'
18+
19+
json = response.parsed_body
20+
21+
assert_equal json, { 'title' => 'post 1' }
22+
end
1523
end
1624

1725
end

0 commit comments

Comments
 (0)