首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONAPI资源路由未显示

JSONAPI资源路由未显示
EN

Stack Overflow用户
提问于 2015-08-23 00:34:20
回答 2查看 1.1K关注 0票数 1

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

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

代码语言:javascript
复制
# 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路由。

EN

回答 2

Stack Overflow用户

发布于 2015-08-23 01:24:05

jsonapi_resources无法工作的最可能原因是您还必须从JSONAPI::ResourceController派生Application Controller

代码语言:javascript
复制
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

代码语言:javascript
复制
Rails.application.routes.draw do
  # other routes here

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

这是一个使用jsonapi_resourcesdemo application

票数 2
EN

Stack Overflow用户

发布于 2015-08-23 16:25:32

我设法让它工作起来

代码语言:javascript
复制
# 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仍然可以完美地工作!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32158384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档