Rails路由错误故障排除

时间:2011-03-26 13:44:29

标签: ruby-on-rails ruby-on-rails-3 rails-routing

当我尝试链接到show_members动作

时,我遇到了一个奇怪的错误
Routing Error
No route matches "/groups/1/show_members"

#routes.rb
get 'groups/:id/members' => 'groups#show_members'

#groups_controller.rb
def show_members

当我运行rake路线时,我得到了这个:

#rake routes
            groups GET    /groups(.:format)           {:controller=>"groups", :action=>"index"}
            groups POST   /groups(.:format)           {:controller=>"groups", :action=>"create"}
         new_group GET    /groups/new(.:format)       {:controller=>"groups", :action=>"new"}
        edit_group GET    /groups/:id/edit(.:format)  {:controller=>"groups", :action=>"edit"}
             group GET    /groups/:id(.:format)       {:controller=>"groups", :action=>"show"}
             group PUT    /groups/:id(.:format)       {:controller=>"groups", :action=>"update"}
             group DELETE /groups/:id(.:format)       {:controller=>"groups", :action=>"destroy"}
        group_join GET    /groups/join(.:format)       {:controller=>"groups", :action=>"join"}
 group_remove_user PUT    /groups/remove_user(.:format){:controller=>"groups", :action=>"remove_user"}
                   GET    /groups/:id/members(.:format){:controller=>"groups", :action=>"show_members"}

更新: 但是我想让show_members采取的行动就是显示该组中的所有用户。我希望用户功能和路径保持不变。而现在show_members行动路线到群组,就像展示一样。在我的群组控制器中,我将默认的节目组操作拆分为三个页面,一个用于显示的组配置文件,一个用于该组中的成员用于show_members,另一个用于新闻页面,这将在show_news时显示我明白了。

#groups_controller.rb
def show_members
  @group = Group.find(params[:id])
  @members = @group.users
  @group_admin = User.find(@group.group_admin)
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @group }
  end
end

#rake routes
group_join GET    /groups/:group_id/join(.:format) {:controller=>"groups", :action=>"join"}
group_remove_user PUT    /groups/:group_id/remove_user(.:format) {:controller=>"groups", :action=>"remove_user"}
group GET    /groups/:group_id/:id/members(.:format) {:controller=>"groups", :action=>"show_members"}
group_users GET    /groups/:group_id/users(.:format) {:controller=>"users", :action=>"index"}
group_user GET    /groups/:group_id/users/:id(.:format) {:controller=>"users", :action=>"show"}
I WANT THIS new_user_session GET    /users/sign_in(.:format) {:controller=>"devise/sessions", :action=>"new"}
INSTEAD OF THIS new_user_group_session GET    /users/groups/:group_id/sign_in(.:format)          {:controller=>"devise/sessions", :action=>"new"}
user_group_session POST   /users/groups/:group_id/sign_in(.:format)          {:controller=>"devise/sessions", :action=>"create"}
destroy_user_group_session GET    /users/groups/:group_id/sign_out(.:format)         {:controller=>"devise/sessions", :action=>"destroy"}
user_group_password POST   /users/groups/:group_id/password(.:format)         {:controller=>"devise/passwords", :action=>"create"}
new_user_group_password GET    /users/groups/:group_id/password/new(.:format)     {:controller=>"devise/passwords", :action=>"new"}
edit_user_group_password GET    /users/groups/:group_id/password/edit(.:format)    {:controller=>"devise/passwords", :action=>"edit"}
user_group_password PUT    /users/groups/:group_id/password(.:format)         {:controller=>"devise/passwords", :action=>"update"}
user_group_registration POST   /users/groups/:group_id(.:format)                  {:controller=>"users/registrations", :action=>"create"}

new_user_group_registration GET /users/groups/:group_id/sign_up(.:format) {:controller =>“users / registrations”,:action =>“new”} edit_user_group_registration GET /users/groups/:group_id/edit(.:format) {:controller =>“users / registrations”,:action =>“edit”}      user_group_registration PUT /users/groups/:group_id(.:format) {:controller =>“users / registrations”,:action =>“update”}      user_group_registration DELETE /users/groups/:group_id(.:format) {:controller =>“users / registrations”,:action =>“destroy”}      user_group_confirmation POST /users/groups/:group_id/confirmation(.:format) {:controller =>“devise / confirmations”,:action =>“create”}  new_user_group_confirmation GET /users/groups/:group_id/confirmation/new(.:format) {:controller =>“devise / confirmations”,:action =>“new”}      user_group_confirmation GET /users/groups/:group_id/confirmation(.:format) {:controller =>“devise / confirmations”,:action =>“show”}      groups GET / groupss(。:format){:controller =>“groups”,:action =>“index”}      groups POST /groups(。:format){:controller =>“groups”,:action =>“create”}      new_group GET /groups/new(.:format){:controller =>“groups”,:action =>“new”}      edit_group GET /groups/:id/edit(.:format) {:controller =>“groups”,:action =>“edit”}      group GET /groups/:id(.:format){:controller =>“groups”,:action =>“show”}      group PUT /groups/:id(.:format){:controller =>“groups”,:action =>“update”}      group DELETE /groups/:id(.:format){:controller =>“groups”,:action =>“destroy”}

#routes.rb
resources :groups do
  get 'join' => 'groups#join'
  put 'remove_user' => 'groups#remove_user'
  get ':id/members' => 'groups#show_members'
  resources :users, :only => [:index, :show]
  devise_for :users, :controllers => { :registrations => "users/registrations" }
end

2 个答案:

答案 0 :(得分:2)

当Rails表示没有路由匹配/groups/1/show_members时,Rails是正确的 - 您的routes.rb正在创建/groups/1/members之类的路由(不包含“show_”)

您需要将routes.rb更改为:

get 'groups/:id/show_members` => 'groups#show_members'

答案 1 :(得分:0)

问题当然是你没有做好一切。

resources :groups do |group|
  group.resources :members
end

link_to @member.name, [@group, @member]

魔术。

现在阅读指南是一个非常好的主意。

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