首页 > 解决方案 > Rails 路线消歧

问题描述

Rails 版本:5.2.3 Ruby 版本:2.0.4

在下文中,我使用对 post 的关注来克服对 get 施加的大小限制。它最终与#show 发生冲突,我不知道如何消除它们的歧义。

路线.rb

require 'sidekiq/web'

Rails.application.routes.draw do
    concern :with_datatable do
      post 'datatable', on: :collection
    end
    resources :organizations, concerns: [:with_datatable]
    mount Sidekiq::Web, at: "/ninjas"
end

我的路线如下:

  datatable_organizations POST   /organizations/datatable(.:format)                                                       organizations#datatable
            organizations GET    /organizations(.:format)                                                                 organizations#index
                          POST   /organizations(.:format)                                                                 organizations#create
         new_organization GET    /organizations/new(.:format)                                                             organizations#new
        edit_organization GET    /organizations/:id/edit(.:format)                                                        organizations#edit {:id=>/[0-9]+/}
             organization GET    /organizations/:id(.:format)                                                             organizations#show {:id=>/[0-9]+/}
                          PATCH  /organizations/:id(.:format)                                                             organizations#update {:id=>/[0-9]+/}
                          PUT    /organizations/:id(.:format)                                                             organizations#update {:id=>/[0-9]+/}
                          DELETE /organizations/:id(.:format)                                                             organizations#destroy {:id=>/[0-9]+/}
              sidekiq_web        /ninjas                                                                                  Sidekiq::Web
       rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

导航到路线,/organizations/datatable.json 会产生此错误: 尽管有明确的路线,但OrganizationsController#show is missing a template for this request format and variant. 它认为/organizations/datatable.json是匹配的/organizations/:id/show/organizations/datatable(.:format)

这是我的控制器:

class OrganizationsController < ApplicationController
    def index
        @title              = "Organizations"
        @page_description   = "Organization data warehouse"
        @page_icon          = "institution"
        @organization       = Organization.new
        @load               = {data_table: true}

      respond_to do |format|
        format.html
        format.json { render json: OrganizationDatatable.new(params) }
      end

    end
    def datatable
        respond_to do |format|
            format.json { render json: OrganizationDatatable.new(params) }
        end
    end
    def get_raw_records
        Organization.all
    end
    def create

    end
    def edit

    end
    def destroy

    end
    def show

    end
    def update

    end
    def new

    end
end

完整的错误文本:

ActionController::UnknownFormat - OrganizationsController#show is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

我在这里想念什么?

标签: ruby-on-railsroutes

解决方案


推荐阅读