简体   繁体   中英

Api::V1::CompaniesController#index is missing a template for request formats: text/html

I'm getting this error and I haven't been able to figure out how to fix this. Initially it was going to be just a rest api, but now I've decided to add a front-end as well, so I added the 'slim-rails' gem and tried to create the front-end for my companies_controller but I'm stuck with this error, it works fine if I request a JSON, but for some reason it doesn't find the template and I get "Api::V1::CompaniesController#index is missing a template for request formats: text/html". Most of the research I've done points to wrong names, but apparently everything is fine. Here is my code:

companies_controller.rb

module Api
    module V1
        class CompaniesController < ApplicationController            
            before_action :set_company, only: [:show, :edit, :update, :destroy]
            #List all
            def index
                # TODO validate and filter based on user role permissions
                @companies = Company.order('active DESC, name')
                respond_to do |format|
                    format.html 
                    format.json { json_response(@companies , status: :ok) }
                end
            end

            #other controller methods

            private

            def set_company
                @company = Company.find(params[:id])
            end

            def company_params
                params.require(:company).permit(:id, :name, :active, :city_id)
            end

        end
    end
end 

routes.rb

Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v1' do
      resources :states, only: :index
      resources :companies
    end
  end
end 

index.html.slim

h1 Listing companies

table
  thead
    tr
      th Name
      th Active
      th City
      th
      th
      th

  tbody
    - @companies.each do |company|
      tr
        td = company.name
        td = company.active
        td = company.city
        td = link_to 'Show', company
        td = link_to 'Edit', edit_company_path(company)
        td = link_to 'Destroy', company, data: { confirm: 'Are you sure?' }, method: :delete

br

= link_to 'New Company', new_company_path

Project Structure

项目结构

Because the Controller is in the Api::V1 namespace, Rails will be looking for views in the same namespace, which means it'll be looking for a view in app/views/api/v1/companies/index.html.slim . And that directory of course doesn't exist. You could try creating that directory and moving the file there.

However, I don't think that's a good idea. API Controllers should be separate from view-based Controllers. If you want to keep everything in the same application (both the API and the frontend), I'd suggest creating a new Controller for handling the frontend in the root namespace, eg app/controllers/companies_controller.rb . Then that Controller will just act like a "normal" Rails Controller, rather than an API-specific one.

Or you could do like @max suggested and just have an entirely separate application for the frontend. That would be ideal, but if you're just working on a demo/side project, it might be overkill for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM