1

所以我遇到了一个问题,我有这个应该返回 json 的索引操作,但是在浏览器中我总是得到一个空响应。

控制器

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

rake 路由的输出

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

真正让我失望的是,当我在 rails 控制台的 index 操作中运行相同的代码时,它输出的正是我所期望的。表演动作也很好。只是为了测试,我添加了一个新路由,我刚刚调用index2并将代码从index操作复制 + 粘贴到index2其中,我在浏览器中得到了正确的输出。

所以它必须与我的路线有关,但我不确定它是什么?

编辑 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 回答