Rails路由错误

时间:2012-10-21 17:01:42

标签: ruby-on-rails railstutorial.org

我跟随迈克尔·哈特尔的Ruby on Rails Tutorial 2nd Edition并且已经到达了本书的登录/注销部分。

到目前为止,我可以创建一个新用户(或者在我的情况下为业主)并使用新凭据登录。我遇到的问题是退出时。我点击“注销”并收到路线错误说:

  

没有路线匹配[GET]“/ signout”

以下是代码段。任何帮助将非常感谢!

rake路线输出

landlords GET    /landlords(.:format)           landlords#index
               POST   /landlords(.:format)           landlords#create
  new_landlord GET    /landlords/new(.:format)       landlords#new
 edit_landlord GET    /landlords/:id/edit(.:format)  landlords#edit
      landlord GET    /landlords/:id(.:format)       landlords#show
               PUT    /landlords/:id(.:format)       landlords#update
               DELETE /landlords/:id(.:format)       landlords#destroy
    properties GET    /properties(.:format)          properties#index
               POST   /properties(.:format)          properties#create
  new_property GET    /properties/new(.:format)      properties#new
 edit_property GET    /properties/:id/edit(.:format) properties#edit
      property GET    /properties/:id(.:format)      properties#show
               PUT    /properties/:id(.:format)      properties#update
               DELETE /properties/:id(.:format)      properties#destroy
      sessions POST   /sessions(.:format)            sessions#create
   new_session GET    /sessions/new(.:format)        sessions#new
       session DELETE /sessions/:id(.:format)        sessions#destroy
          root        /                              content_pages#home
content_pages_home GET    /content_pages/home(.:format)  content_pages#home
          help        /help(.:format)                content_pages#help
     questions        /questions(.:format)           content_pages#questions
        signup        /signup(.:format)              landlords#new
        signin        /signin(.:format)              sessions#new
       signout DELETE /signout(.:format)             sessions#destroy

routes.rb文件

  resources :landlords
  resources :properties
  resources :sessions, only: [:new, :create, :destroy]

  root :to => 'content_pages#home'

  get "content_pages/home"

  match '/help', to: 'content_pages#help'
  match '/questions', to: 'content_pages#questions'
  match '/signup', to: 'landlords#new'
  match '/signin', to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

链接到退出

<%= link_to "Signout", signout_path, method: "delete" %>

会话控制器

def destroy
  sign_out
  redirect_to root_path
end

1 个答案:

答案 0 :(得分:4)

以下代码中的via:选项限制了对delete方法的请求:

match '/signout', to: 'sessions#destroy', via: :delete

你需要制作一个与'get'方法一起使用的方法

查看Rails Routing guide