1

actually i use rails for my REST API, and i need transform my object to json but when i try i got this error:

            <h1>Template is missing</h1>
        <p>Missing template firms/show, application/show with {:locale=&gt;[:en], :formats=&gt;[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=&gt;[:erb, :builder, :arb, :jbuilder]}. Searched in:
  * &quot;/Users/allan/Desktop/Work/back/app/views&quot;
  * &quot;/Library/Ruby/Gems/2.0.0/gems/activeadmin-0.6.0/app/views&quot;
  * &quot;/Library/Ruby/Gems/2.0.0/gems/kaminari-0.16.3/app/views&quot;
  * &quot;/Library/Ruby/Gems/2.0.0/gems/devise_invitable-1.5.5/app/views&quot;
  * &quot;/Library/Ruby/Gems/2.0.0/gems/devise-3.5.4/app/views&quot;
</p>

This is my code

 def show
        firm= Firm.find_by_subdomain(params[:subdomain])
        if firm.present?
          respond_to do |format|
            @firm = firm
            format.json { render :json => @firm.to_json }
          end
        end
      end

I hope someone here can help me :)

Solve:

  def show
    render json: Firm.find_by_subdomain(current_subdomain)
  end

thank you

2 Answers2

1

Template missing means that you asking for a html view, not doing a json request.

If you want to always return json format regardless of format param, do this:

before_action :set_default_response_format

protected

def set_default_response_format
  request.format = :json
end

@source: Rails 4 - How to render JSON regardless of requested format?

Community
  • 1
  • 1
0

Try to add .json at the end of you query when querying your route. Without this, it will try to render html and it will go search for view file that might not be present in your case.

born4new
  • 1,677
  • 12
  • 12