Rails 路线错误排查

发布于 2024-10-26 11:46:11 字数 5428 浏览 2 评论 0原文

当我尝试链接到 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 相同。在我的组控制器中,我将默认的显示组操作分为三页,一页用于显示的组配置文件,一页用于该组中的成员,该页面将转到 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=>"用户/注册", :action=>"新"} edit_user_group_registration GET /users/groups/:group_id/edit(.:format) {:controller=>"用户/注册", :action=>"编辑"} user_group_registration PUT /users/groups/:group_id(.:format) {:controller=>"用户/注册", :action=>"更新"} user_group_registration DELETE /users/groups/:group_id(.:format) {:controller=>"用户/注册", :action=>"销毁"} 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 /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"} 组 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

I'm getting a weird error when I try to link to the show_members action

Routing Error
No route matches "/groups/1/show_members"

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

#groups_controller.rb
def show_members

When I run rake routes I get this:

#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"}

UPDATE:
But all I want the show_members action to do is show all the users within that group. I want the user functionality and paths to remain the same. And now the show_members action routes to group, the same as show. In my groups controller, I split the default show group action into three pages, one for the group profile which is show, one for the members in that group which goes to show_members, and one for the news page, which will go to show_news when i get to it.

#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 /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"}

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-11-02 11:46:11

当 Rails 说没有路由匹配 /groups/1/show_members 时,它是正确的 - 您的 routes.rb 正在为 /groups/1/ 之类的内容创建路由成员(不带“show_”)

您需要将routes.rb更改为如下所示:

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

Rails is correct when it says that no route matches /groups/1/show_members - your routes.rb is creating a route for something like /groups/1/members (without the "show_")

You'll need to change routes.rb to look like:

get 'groups/:id/show_members` => 'groups#show_members'
萧瑟寒风 2024-11-02 11:46:11

问题当然是你没有把所有事情都做对。

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

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

魔法。

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

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

Well the problem is of course you haven't done everything right.

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

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

Magic.

Now reading the guide would be a very good idea.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文