Rails索引操作始终返回null

时间:2013-04-14 19:41:01

标签: ruby-on-rails-3 controller routes

所以我遇到一个问题,我有这个应该返回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控制台中运行索引操作中的相同代码时,它正在输出我期望的内容。 show动作也很好。仅仅为了测试,我添加了一个我刚刚调用index2的新路由,并将index操作中的代码复制+粘贴到index2中,并在浏览器中获得了正确的输出。 / p>

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

编辑1:添加佣金路线的整个输出

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

3 个答案:

答案 0 :(得分:2)

您需要沿着路线列表进一步移动此路线。

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

按顺序搜索路线列表。 /admin/groups/users匹配此路线,:id等于users

答案 1 :(得分: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)

答案 2 :(得分:0)

请更改您的路线并检查

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