简体   繁体   English

从某些页面获取路由错误

[英]Getting a routing error from certain pages

I have this route 我有这条路

profile GET /contacts/:id(.:format) {:controller=>"my_devise/contacts", :action=>"profile"}

This is my controllers/application_controller.rb 这是我的controllers / application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :get_current_user

    def get_current_user
        @current_user = current_user
    end

end

If i have this link in my view/layouts/application.html.erb file <%= link_to "Profile", profile_path(@current_user) %> on the url http://localhost:3000/contacts/1 , i have no errors, but if i try to hit the url http://localhost:3000/contacts , I get the error below 如果我的view / layouts / application.html.erb文件中的此链接<%= link_to "Profile", profile_path(@current_user) %> URL http://localhost:3000/contacts/1上的<%= link_to "Profile", profile_path(@current_user) %> ,我没有错误,但是如果我尝试点击url http://localhost:3000/contacts ,则会收到以下错误

Routing Error

No route matches {:controller=>"my_devise/contacts", :action=>"profile"}

If i remove the link in my application.html.erb file and hit http://localhost:3000/contacts , the error goes away. 如果我删除了application.html.erb文件中的链接并点击http://localhost:3000/contacts ,则错误消失了。

Why would that link cause this error? 为什么该链接会导致此错误?

EDIT 编辑

Full routes file 完整路线文件

$ rake routes
          users_sign_out GET    /users/sign_out(.:format)      {:controller=>"devise/sessions", :action=>"destroy"}
           users_sign_in GET    /users/sign_in(.:format)       {:controller=>"my_devise/sessions", :action=>"new"}
                    home GET    /home(.:format)                {:action=>"index", :controller=>"my_devise/sessions"}
                contacts GET    /contacts(.:format)            {:action=>"list", :controller=>"my_devise/contacts"}
                 profile GET    /contacts/:id(.:format)        {:controller=>"my_devise/contacts", :action=>"profile"}
            edit_profile GET    /contacts/:id/edit(.:format)   {:controller=>"my_devise/contacts", :action=>"edit"}
                         POST   /contacts/:id/edit(.:format)   {:controller=>"my_devise/contacts", :action=>"update_user"}
                    more GET    /more/:id(.:format)            {:controller=>"my_devise/contacts", :action=>"more"}
                         POST   /home(.:format)                {:action=>"create_new_user", :controller=>"my_devise/sessions"}
           users_sign_up GET    /users/sign_up(.:format)       {:controller=>"my_devise/registrations", :action=>"new"}
                         POST   /users/sign_up(.:format)       {:controller=>"my_devise/registrations", :action=>"new"}
        new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
        new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"my_devise/sessions"}
                         POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"my_devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"my_devise/sessions"}
                         POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
                         GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
                         GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
                         GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"my_devise/registrations"}
                         POST   /users(.:format)               {:action=>"create", :controller=>"my_devise/registrations"}
                         GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"my_devise/registrations"}
                         GET    /users/edit(.:format)          {:action=>"edit", :controller=>"my_devise/registrations"}
                         PUT    /users(.:format)               {:action=>"update", :controller=>"my_devise/registrations"}
                         DELETE /users(.:format)               {:action=>"destroy", :controller=>"my_devise/registrations"}
              home_index GET    /home/index(.:format)          {:controller=>"home", :action=>"index"}
                    root        /                              {:controller=>"home", :action=>"index"}
                    root        /                              {:controller=>"home", :action=>"index"}

To answer your actual question, it's because you don't have a route for that ( /contacts - note no id). 要回答您的实际问题,这是因为您没有为此的路线( /contacts注意没有ID)。 Your route is /contacts/:id(.:format) - the format is optional, but the id is not. 您的路线是/contacts/:id(.:format)格式是可选的,但id不是。 You'll need to make the id optional, too, or create another route. 您还需要将ID设置为可选,或创建其他路由。

The routes is like this 路线是这样的

contacts GET /contacts(.:format) {:action=>"list", :controller=>"my_devise/contacts"} 联系人GET /contacts(.:format){:action =>“列表”,:controller =>“ my_devise / contacts”}

Here we have to give the format also. 在这里,我们还必须提供格式。 If we give contacts it will throw routing error. 如果我们给联系人,它将引发路由错误。 So please enter the format also. 因此,请同时输入格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM