1

そのため、json を返すはずのこのインデックス アクションがあるという問題が発生していますが、ブラウザーでは常に null 応答が返されます。

コントローラ

class Admin::Groups::UsersController < Admin::GroupsController
  def index
    @user_groups = Group.includes(:group_type, :users).where("group_types.group_type_name = ?", 'Users').order("groups.group_name ASC")

    render json: @user_groups.to_json(:include => [:group_type, :users], :methods => :enabled)
  end

  def show
    @user_group = Group.includes(:group_type, :users).where("group_types.group_type_name = ?", 'Users').find_by_id(params[:id])

    render json: @user_group.to_json(:include => [:group_type, :users], :methods => :enabled)
  end
end

ルートファイル

namespace :admin do
  namespace :groups do
    resources :users, only: [:index, :show] do
      get 'enabled', :on => :collection
      get 'disabled', :on => :collection
    end
  end
end

レーキルートの出力

admin_groups_users GET    /admin/groups/users(.:format)                   admin/groups/users#index

本当に私を悩ませているのは、Rails コンソールの index アクション内にある同じコードを実行すると、期待したとおりに出力されることです。また、ショーアクションは正常に機能します。テストのために、呼び出したばかりの新しいルートを追加し、アクションindex2からコードをコピーして貼り付けたところ、ブラウザに正しい出力が得られました。indexindex2

だからそれは私のルートと関係があるはずですが、それが何であるかわかりませんか?

編集 1: rake ルートの出力全体を追加

enabled_admin_accounts GET    /admin/accounts/enabled(.:format)               admin/accounts#enabled

disabled_admin_accounts GET    /admin/accounts/disabled(.:format)              admin/accounts#disabled

account_owners_admin_accounts GET    /admin/accounts/account_owners(.:format)  admin/accounts#account_owners

admin_accounts GET    /admin/accounts(.:format)                       admin/accounts#index

admin_account GET    /admin/accounts/:id(.:format)                   admin/accounts#show

enabled_admin_users GET    /admin/users/enabled(.:format)                  admin/users#enabled

disabled_admin_users GET    /admin/users/disabled(.:format)                 admin/users#disabled

admin_users GET    /admin/users(.:format)                          admin/users#index

admin_user GET    /admin/users/:id(.:format)                      admin/users#show
enabled_admin_owners GET    /admin/owners/enabled(.:format)                 admin/owners#enabled

disabled_admin_owners GET    /admin/owners/disabled(.:format)                admin/owners#disabled

currencies_admin_owner GET    /admin/owners/:id/currencies(.:format)          admin/owners#currencies

admin_owners GET    /admin/owners(.:format)                         admin/owners#index

admin_owner GET    /admin/owners/:id(.:format)                     admin/owners#show

enabled_admin_counterparties GET    /admin/counterparties/enabled(.:format)         admin/counterparties#enabled

disabled_admin_counterparties GET    /admin/counterparties/disabled(.:format)        admin/counterparties#disabled

admin_counterparties GET    /admin/counterparties(.:format)                 admin/counterparties#index

admin_counterparty GET    /admin/counterparties/:id(.:format)             admin/counterparties#show

enabled_admin_venues GET    /admin/venues/enabled(.:format)                 admin/venues#enabled

disabled_admin_venues GET    /admin/venues/disabled(.:format)                admin/venues#disabled

admin_venues GET    /admin/venues(.:format)                         admin/venues#index

admin_venue GET    /admin/venues/:id(.:format)                     admin/venues#show

enabled_admin_custody_banks GET    /admin/custody_banks/enabled(.:format)          admin/custody_banks#enabled

disabled_admin_custody_banks GET    /admin/custody_banks/disabled(.:format)         admin/custody_banks#disabled

admin_custody_banks GET    /admin/custody_banks(.:format)                  admin/custody_banks#index

admin_custody_bank GET    /admin/custody_banks/:id(.:format)              admin/custody_banks#show

enabled_admin_client_currencies GET    /admin/client_currencies/enabled(.:format)      admin/client_currencies#enabled

disabled_admin_client_currencies GET    /admin/client_currencies/disabled(.:format)     admin/client_currencies#disabled

admin_client_currencies GET    /admin/client_currencies(.:format)              admin/client_currencies#index

admin_client_currency GET    /admin/client_currencies/:id(.:format)          admin/client_currencies#show

admin_currencies        /admin/currencies(.:format)                     admin/client_currencies#index

admin_all_currencies        /admin/all_currencies(.:format)                 admin/currencies#index

enabled_admin_groups GET    /admin/groups/enabled(.:format)                 admin/groups#enabled

disabled_admin_groups GET    /admin/groups/disabled(.:format)                admin/groups#disabled

admin_groups GET    /admin/groups(.:format)                         admin/groups#index

admin_group GET    /admin/groups/:id(.:format)                     admin/groups#show

enabled_admin_groups_accounts GET    /admin/groups/accounts/enabled(.:format)        admin/groups/accounts#enabled

disabled_admin_groups_accounts GET    /admin/groups/accounts/disabled(.:format)       admin/groups/accounts#disabled

admin_groups_accounts GET    /admin/groups/accounts(.:format)                admin/groups/accounts#index

admin_groups_account GET    /admin/groups/accounts/:id(.:format)            admin/groups/accounts#show

enabled_admin_groups_account_owners GET    /admin/groups/account_owners/enabled(.:format)  admin/groups/account_owners#enabled

disabled_admin_groups_account_owners GET    /admin/groups/account_owners/disabled(.:format) admin/groups/account_owners#disabled

admin_groups_account_owners GET    /admin/groups/account_owners(.:format)          admin/groups/account_owners#index

admin_groups_account_owner GET    /admin/groups/account_owners/:id(.:format)      admin/groups/account_owners#show

enabled_admin_groups_users GET    /admin/groups/users/enabled(.:format)           admin/groups/users#enabled

disabled_admin_groups_users GET    /admin/groups/users/disabled(.:format)          admin/groups/users#disabled

admin_groups_users GET    /admin/groups/users(.:format)                   admin/groups/users#index

admin_groups_user GET    /admin/groups/users/:id(.:format)               admin/groups/users#show

enabled_admin_groups_counterparties GET    /admin/groups/counterparties/enabled(.:format)  admin/groups/counterparties#enabled

disabled_admin_groups_counterparties GET    /admin/groups/counterparties/disabled(.:format) admin/groups/counterparties#disabled

admin_groups_counterparties GET    /admin/groups/counterparties(.:format)          admin/groups/counterparties#index

admin_groups_counterparty GET    /admin/groups/counterparties/:id(.:format)      admin/groups/counterparties#show

enabled_admin_groups_currencies GET    /admin/groups/currencies/enabled(.:format)      admin/groups/currencies#enabled

disabled_admin_groups_currencies GET    /admin/groups/currencies/disabled(.:format)     admin/groups/currencies#disabled

admin_groups_currencies GET    /admin/groups/currencies(.:format)              admin/groups/currencies#index

admin_groups_currency GET    /admin/groups/currencies/:id(.:format)          admin/groups/currencies#show

root        /                                               trades#index

jasminerice        /jasmine                                        Jasminerice::Engine
4

3 に答える 3

2

このルートをルート リストのさらに下に移動する必要があります。

admin_group GET    /admin/groups/:id(.:format)                     admin/groups#show

ルートリストは順番に検索されます。/admin/groups/usersは、このルートと:id等しい に一致しusersます。

于 2013-04-14T20:34:13.933 に答える
1

render次のいずれかの形式です

render :json => @user_groups

あなたを呼び出しto_json、引数を渡すことができない場合、またはこれを行うことができます:

format.json { render @user_groups.to_json(:include => [:group_type, :users], :methods => :enabled) }

何があっても常にjsonを返したい場合formatは、次のことを取り除いてjson:ください。

render @user_groups.to_json(:include => [:group_type, :users], :methods => :enabled)
于 2013-04-14T19:47:27.180 に答える
0

ルートをこれに変更して確認してください

namespace :admin do
        namespace :groups do
          resources :users, :only => [:index,:show],:collection => {:enabled => :get,:disabled => :get}
        end
      end
于 2013-04-14T20:10:25.133 に答える