使用rails_admin和rails_api

10

我最初在rails_api的GitHub上发布了一个问题,但由于没有活动,现在我在这里发布。

我正在尝试在Rails 5 API应用程序中使用rails_admin。我包含了额外的ActionController模块,直到我可以拥有一个功能齐全的rails_admin面板或可工作的API请求。问题似乎是rails_admin依赖于ActionView::Layouts,一旦包含就会对API请求造成影响。

Gemfile:

gem 'rails', '>= 5.0.0.beta3', '< 5.1'
...
gem 'rack-pjax', github: 'afcapel/rack-pjax'
gem 'remotipart', github: 'mshibuya/remotipart'
gem 'kaminari', github: 'amatsuda/kaminari', branch: '0-17-stable'
gem 'rails_admin', github: 'sferik/rails_admin'

我配置了我的应用程序使用ActionDispatch::Flash

module MyApp
  class Application < Rails::Application
    ...
    config.middleware.use ActionDispatch::Flash
  end
end

我为Rails API配置了额外的模块,如ApplicationController:

选择控制器模块

class ApplicationController < ActionController::API
  include Knock::Authenticatable
  include Pundit

  # RailsAdmin support
  include AbstractController::Helpers
  include ActionController::Flash
  include ActionController::RequestForgeryProtection
  include ActionController::MimeResponds
  include ActionController::HttpAuthentication::Basic::ControllerMethods
  include ActionView::Layouts
end

经过这些更改,Rails Admin控制台似乎可以正常运行。然而,当我尝试访问应用程序中的JSON资源时,会抛出以下错误:

Error:
BookingsControllerTest#test_should_get_index:
ActionView::MissingTemplate: Missing template bookings/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :haml]}. Searched in:
  * "/Users/richard/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rails_admin-355dc80f8a20/app/views"

测试代码(尝试添加format: :json):
class BookingsControllerTest < ActionController::TestCase
  test 'should get index' do
    get :index

    assert_response :success
  end
end

这是控制器的代码:

class BookingsController < ApplicationController
  def index
    @bookings = find_bookings
    render json: @bookings, include: ['customer', 'client'], meta: meta
  end
end

只有当我在顶层ActionController::API类中包含ActionView::Layouts模块以支持Rails Admin时,才会发生这种情况。


你的BookingsControllerTest里面有什么?我认为了解这个会有帮助,因为问题就出在那里... - Uzbekjon
你尝试使用 ActiveModel::Serializers 吗?这是 Rails-API 推荐的。 - trueinViso
3个回答

8
如果我是你,我会尝试隔离API和RailsAdmin控制器。我认为这一定会起作用:
class ApplicationController < ActionController::API
  include Knock::Authenticatable
  include Pundit
end

class RailsAdminCustomController < ApplicationController
  # RailsAdmin support
  include AbstractController::Helpers
  include ActionController::Flash
  include ActionController::RequestForgeryProtection
  include ActionController::MimeResponds
  include ActionController::HttpAuthentication::Basic::ControllerMethods
  include ActionView::Layouts
end

config/initializers/rails_admin.rb中。
RailsAdmin.config do |config|
  # other config stuff ...
  config.parent_controller = '::RailsAdminCustomController'
end

只需在此处检查RailsAdmin :: ApplicationController 这里,以及配置设置这里


3

这是真的!同时,我已经采取更新rails_admin并删除上述自定义解决方案的措施。 - richard
如果我没记错的话,它在1.1.0版本中是可以工作的;但现在在Rails 5.1.2和rails_admin 1.2中,我得到了ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):的错误。 - Saravanabalagi Ramachandran
是的,没错。这个问题还存在着,我在这里进行了描述。 - Carlos Ramirez III
Devise和它的sign_in视图怎么样? - Dachi Natsvlishvili

0

你应该在这个位置bookings/index.json.jbuilder中有一个json视图文件,文件内应该包含如下内容:

bookings/index.json.jbuilder

json.name @bookings.name
json.date @bookings.date

这是另一个缺失的模板

application/index

但我真的不完全了解你的应用程序。所以也许这就是你使用ActionView::Layouts实现的应用程序布局。如果是这种情况,它要求你在位置application/index中实现一个布局页面文件。

注意:这两个文件都在views文件夹中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接