没有路由匹配控制器(Rails 3)

时间:2013-08-19 09:21:11

标签: ruby-on-rails-3

我对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路由输出

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>

的routes.rb

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

devise_for:users

GroupsController.rb:

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

是:     def指数         @user = current_user         @groups = Group.all     端

UsersController.rb

class Devise::UsersController < DeviseController
   def show
   end
end

2 个答案:

答案 0 :(得分:0)

我认为这很明显。你的

中有拼写错误
<li><%= link_to 'My groups', user_groups_path %></li>

链接。路径应该是user_group_path(没有's'),如rake routes输出中所示,而不是您在链接中写的那个。

答案 1 :(得分:0)

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

对于Ex:如果我有user1

然后我将使用这些路线:

link_to 'My groups', user_groups_path(user1)

OR

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

link_to 'My Group', new_user_group_path(user1)

OR

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

这是告诉您,您要为哪个用户创建群组。

如果你想访问任何特定的组,请说:ex:grp1

然后我的网址将是

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

这是对路由的良好参考。

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