Rails: Render JSON after Controller action results in "missing a template for this request format and variant" error

Viewed 328

I have this simple Admin controller and I'm trying to display a message after the #reset action is called to give the admin feedback when the action is completed (as it executes de seed.rb file)

The action performs just fine, but I can't get it to display the msg and I get this message in the server log:

ActionController::UnknownFormat (Api::V1::AdminController#reset is missing a template for this request format and variant.

request.formats: ["application/json"]
request.variant: []):


class Api::V1::AdminController < Api::V1::BaseController
  before_action :initialize

  def seed
    @builder.process_file
  end

  def reset
    Rails.application.load_seed do
      msg = { :status => "ok" }
      respond_to do |format|
           format.json  { render :json => msg }
         end
    end
  end

  private

  def initialize
    @builder = PaymentBuilder.new
  end
end

What am I doing wrong?

EDIT:

I had to declare respond_to :json at the class level of the controller and change the action block to

  def reset
    Rails.application.load_seed
    msg = { :reset => "OK" }
      respond_with do |format|
        format.json  { render :json => msg }
      end
  end

And it renders fine now.

1 Answers

I guess this is happening because you are rendering inside of the block, I think you can not do that in that way

Related