0

我正在关注 Michael Hartl 的Ruby on Rails 教程第 2 版,并已到达本书的登录/注销部分。

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

没有路线匹配 [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

路由.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
4

1 回答 1

4

以下代码中的via:选项将请求限制为删除方法:

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

您需要制作一个适用于“get”方法的

查看Rails 路由指南

于 2012-10-21T17:16:57.580 回答