【问题标题】:JSONAPI Resource routes not showing upJSONAPI 资源路由未显示
【发布时间】:2015-08-23 16:25:31
【问题描述】:

我正在尝试使用 JSONAPI Resource gem 为我的 rails 应用程序创建 API。我希望能够在我的控制器中利用正常的 Rails 路由,并拥有一个命名空间 API。

到目前为止,我有类似的东西

# sitting in resources/api/goal_resource.rb
module Api
  class GoalResource < JSONAPI::Resource
    attributes :name, :description, :progress
  end
end

Rails.application.routes.draw do
  devise_for :users,
    path: '',
  controllers: {
    registrations: 'users/registrations',
    invitations: 'users/invitations',
    sessions: 'users/sessions'
  },
  path_names: {
    edit:  'settings/profile'
  }

  devise_scope :user do
    authenticated :user do
      root 'dashboard#index', as: :authenticated_root
    end

    unauthenticated do
      root 'users/sessions#new', as: :unauthenticated_root
    end
  end

  post '/invitation/:id/resend', to: 'users/invitations#resend', as: :resend_invitation

  resources :goals do
    resources :goal_comments
    resources :goal_followers, only: [:index]
    member do
      post :on_target, as: :on_target
    end
  end

  resources :users, path: '/settings/users', only: [:index, :update, :edit, :destroy]
  resources :teams, path: '/settings/teams', only: [:index, :new, :create, :update, :edit, :destroy]
  resources :notifications, only: [:index]

  get "my_goals", to: "my_goals#index", as: :my_goals
  get "user_goals/:user_id", to: "user_goals#index", as: :user_goals
  get "team_goals/:team_id", to: "team_goals#index", as: :team_goals

  namespace :api, defaults: { format: 'json' } do
    jsonapi_resources :goals
  end
end


# Gemfile
source 'https://rubygems.org'
ruby '2.1.2'
gem 'jsonapi-resources'
# other gems here

# models/goal.rb
class Goal < ActiveRecord::Base
  # some more code here
end

是否可以将此 gem 与普通路由结合使用?我究竟做错了什么? rake routes 返回我的应用程序的所有路由,但没有 api 路由。

【问题讨论】:

    标签: ruby-on-rails ruby jsonapi-resources


    【解决方案1】:

    jsonapi_resources 不起作用的最可能原因是您还必须从 JSONAPI::ResourceController 派生您的 Application Controller

    class ApplicationController < JSONAPI::ResourceController
      # Prevent CSRF attacks by raising an exception.
      # For APIs, you may want to use :null_session instead.
      protect_from_forgery with: :null_session
    end
    

    另一件事是(这不会导致您的路线消失),在路线中使用资源的复数形式。使用goals,如下所示而不是goal

    Rails.application.routes.draw do
      # other routes here
    
      namespace :api, defaults: { format: 'json' } do
        jsonapi_resources :goals
      end
    end
    

    这是一个使用jsonapi_resourcesdemo application

    【讨论】:

    • 谢谢,似乎 jsonapi_resources gem 不能与与 API 无关的路由一起运行。这个对吗?现在我的应用程序JSONAPI: Could not find resource 'dashboard'. (Class DashboardResource not found) 中的所有其他路由都出现错误。我已经更新了我的路线,所以你可以明白我的意思。注意我不希望我的仪表板使用 jsonapi_resources。
    • 是的,我认为它们只针对 API 应用程序。当你测试它不起作用时。但是,我不是 100% 确定的。找不到任何明显的来源告诉它!但是,看看 gihtub 上的一些代码,似乎它们是针对仅 API 应用程序的。
    【解决方案2】:

    我设法让它工作

    # app/controllers/api/api_controller.rb
    module Api
      class ApiController < JSONAPI::ResourceController
      end
    end
    
    # app/controllers/api/goals_controller.rb
    module Api
      class GoalsController < ApiController
      end
    end
    

    我刚刚创建了一个单独的 ApiController 并继承自 JSONAPI::ResourceController 然后我为我的目标创建了一个额外的控制器。

    我正常的controllers/goals_controller.rb 仍然可以正常工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 2020-09-04
      • 2021-02-04
      相关资源
      最近更新 更多