diff --git a/lib/simple_json/simple_json_renderable.rb b/lib/simple_json/simple_json_renderable.rb index eaff277..c583e63 100644 --- a/lib/simple_json/simple_json_renderable.rb +++ b/lib/simple_json/simple_json_renderable.rb @@ -29,7 +29,7 @@ def render_to_body(options) # use super when any of [:body, :plain, :html] exist in options return super if self.class::RENDER_FORMATS_IN_PRIORITY.any? { |key| options.key? key } - template_name = template_name(options[:template] || action_name) + template_name = template_name(options[:template] || options[:action] || action_name) if options.key?(:json) process_options(options) @rendered_format = 'application/json; charset=utf-8' diff --git a/test/dummy_app/app/controllers/api/posts_controller.rb b/test/dummy_app/app/controllers/api/posts_controller.rb index 3722553..a0f0a9b 100644 --- a/test/dummy_app/app/controllers/api/posts_controller.rb +++ b/test/dummy_app/app/controllers/api/posts_controller.rb @@ -8,6 +8,12 @@ class PostsController < ActionController::API def show(id) @post = Post.find id end + + def renamed_show(id) + @post = Post.find id + + render :show + end end end end diff --git a/test/dummy_app/config/routes.rb b/test/dummy_app/config/routes.rb index f95f929..87c4299 100644 --- a/test/dummy_app/config/routes.rb +++ b/test/dummy_app/config/routes.rb @@ -20,6 +20,7 @@ if Rails::VERSION::MAJOR >= 5 namespace :api do resources :posts, only: :show + get '/renamed_posts/:id', to: 'posts#renamed_show' end end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html diff --git a/test/features/action_controller_api_test.rb b/test/features/action_controller_api_test.rb index 76871f0..cb72c13 100644 --- a/test/features/action_controller_api_test.rb +++ b/test/features/action_controller_api_test.rb @@ -12,6 +12,14 @@ class ActionControllerAPITest < ActionDispatch::IntegrationTest assert_equal json, { 'title' => 'post 1' } end + + test 'The renamed template correctly renders a JSON' do + get '/api/renamed_posts/1.json' + + json = response.parsed_body + + assert_equal json, { 'title' => 'post 1' } + end end end