1

我对 RoR 很陌生,请帮我确定我错在哪里

我收到以下错误

Routing Error
No route matches {:controller=>"groups"}
Try running rake routes for more information on available routes

尝试渲染以下视图时

<li><%= link_to 'My groups', user_groups_path %></li>
<li><%= link_to 'New group', new_user_group_path %></li>

这是'routes.rb'和rake routes输出

devise_for :users 
resources :users do |user|
  resources :groups do |group| 
    resources :people do |person|
    end
  end
end
              new_user_session GET    /users/sign_in(.:format)                                                                 devise/sessions#new
                  user_session POST   /users/sign_in(.:format)                                                                 devise/sessions#create
          destroy_user_session GET    /users/sign_out(.:format)                                                                devise/sessions#destroy
                 user_password POST   /users/password(.:format)                                                                devise/passwords#create
             new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
            edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                               PUT    /users/password(.:format)                                                                devise/passwords#update
      cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
             user_registration POST   /users(.:format)                                                                         devise/registrations#create
         new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
        edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
                               PUT    /users(.:format)                                                                         devise/registrations#update
                               DELETE /users(.:format)                                                                         devise/registrations#destroy
       user_group_people GET    /users/:user_id/groups/:group_id/people(.:format)                            people#index
                               POST   /users/:user_id/groups/:group_id/people(.:format)                            people#create
   new_user_group_person GET    /users/:user_id/groups/:group_id/people/new(.:format)                        people#new
  edit_user_group_person GET    /users/:user_id/groups/:group_id/people/:id/edit(.:format)                   people#edit
       user_group_person GET    /users/:user_id/groups/:group_id/people/:id(.:format)                        people#show
                               PUT    /users/:user_id/groups/:group_id/people/:id(.:format)                        people#update
                               DELETE /users/:user_id/groups/:group_id/people/:id(.:format)                        people#destroy
             groups GET    /users/:user_id/groups(.:format)                                                   groups#index
                               POST   /users/:user_id/groups(.:format)                                                   groups#create
          new_user_group GET    /users/:user_id/groups/new(.:format)                                               groups#new
         edit_user_group GET    /users/:user_id/groups/:id/edit(.:format)                                          groups#edit
              user_group GET    /users/:user_id/groups/:id(.:format)                                               groups#show
                               PUT    /users/:user_id/groups/:id(.:format)                                               groups#update
                               DELETE /users/:user_id/groups/:id(.:format)                                               groups#destroy
                         users GET    /users(.:format)                                                                         users#index
                               POST   /users(.:format)                                                                         users#create
                      new_user GET    /users/new(.:format)                                                                     users#new
                     edit_user GET    /users/:id/edit(.:format)                                                                users#edit
                          user GET    /users/:id(.:format)                                                                     users#show
                               PUT    /users/:id(.:format)                                                                     users#update
                               DELETE /users/:id(.:format)                                                                         users#destroy

这是'model.rb'

class User < ActiveRecord::Base
has_many :groups

class Group < ActiveRecord::Base
belongs_to :user
has_many :people

请帮我弄清楚如何解决这个问题

非常感谢。

UPD

这是解决方案,它有效我进行了@Abibullah 建议的所有更改以及控制器中的两项更改

看法:

<li><%= link_to 'My groups', user_groups_path(current_user) %></li>
<li><%= link_to 'New group', new_user_group_path(current_user) %></li>

路线.rb

resources :users do |user|
    resources :groups do |group| 
      resources :people 
    end
end

devise_for :用户

GroupsController.rb:

def index
@user = current_user
@user.groups = Group.all

是:def index @user = current_user @groups = Group.all end

用户控制器.rb

class Devise::UsersController < DeviseController
   def show
   end
end
4

2 回答 2

0

我认为这很明显。你有一个错字

<li><%= link_to 'My groups', user_groups_path %></li>

关联。路径应该是user_group_path(不带“s”),如您的rake routes输出中所示,而不是您在链接中编写的路径。

于 2013-08-19T09:37:16.313 回答
0

要在用户内部创建嵌套组,U 需要传递 U 正在创建组的 user_id。

例如:如果我有 user1

然后我将使用这样的路线:

link_to 'My groups', user_groups_path(user1)

或者

link_to 'My groups', user_groups_path(user1.id)

link_to 'My Group', new_user_group_path(user1)

或者

link_to 'My Group', new_user_group_path(user1.id)

这是为了告诉您,您正在为哪个用户创建组。

如果你想访问任何特定的组,例如:grp1

那么我的网址将是

link_to 'My Group', user_group_path(user1, grp1)

这是路由的一个很好的参考。

http://guides.rubyonrails.org/routing.html

于 2013-08-19T11:19:58.107 回答