【问题标题】:Ruby on rails routes doesn't redirect me to the linkRuby on rails 路线不会将我重定向到链接
【发布时间】:2022-08-23 01:47:14
【问题描述】:

我已经学习 Ruby on rails 4 天了?通过 Youtube 上的视频,一切都很好,直到尝试创建链接以修改用户信息并从会话中注销。 我安装了设计并得到以下路线。我为新用户会话并且和新用户注册他们工作正常,但出于某种原因,链接销毁用户会话编辑用户注册不工作。 这是我的代码:

      <%= link_to \"Sign Out\", destroy_user_session_path, method: :delete %>  
      <%= link_to \"Edit profile\", edit_user_registration_path %>  

当我单击链接退出时,我收到以下错误:没有路由匹配 [GET] \"/users/sign_out\"

所以即使我指定了method: :delete,错误说溃败与GET方法不匹配?

如果是编辑用户注册,我被重定向到用户/注册页面。如果我尝试在搜索栏中写入整个地址(如 .../users/edit),它会更改为 users/sign_up。

如果有人可以帮助我,我将非常感激,因为我现在完全迷失了...... 先感谢您。

    rails routes
                                  Prefix Verb   URI Pattern                                                                                       Controller#Action
                        new_user_session GET    /users/sign_in(.:format)                                                                          devise/sessions#new
                            user_session POST   /users/sign_in(.:format)                                                                          devise/sessions#create
                    destroy_user_session DELETE /users/sign_out(.:format)                                                                         devise/sessions#destroy
                       new_user_password GET    /users/password/new(.:format)                                                                     devise/passwords#new
                      edit_user_password GET    /users/password/edit(.:format)                                                                    devise/passwords#edit
                           user_password PATCH  /users/password(.:format)                                                                         devise/passwords#update
                                         PUT    /users/password(.:format)                                                                         devise/passwords#update
                                         POST   /users/password(.:format)                                                                         devise/passwords#create
                cancel_user_registration GET    /users/cancel(.:format)                                                                           devise/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                                                                          devise/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                                                                             devise/registrations#edit
                       user_registration PATCH  /users(.:format)                                                                                  devise/registrations#update
                                         PUT    /users(.:format)                                                                                  devise/registrations#update
                                         DELETE /users(.:format)                                                                                  devise/registrations#destroy
                                         POST   /users(.:format)                                                                                  devise/registrations#create
                                 friends GET    /friends(.:format)                                                                                friends#index
                                         POST   /friends(.:format)                                                                                friends#create
                              new_friend GET    /friends/new(.:format)                                                                            friends#new
                             edit_friend GET    /friends/:id/edit(.:format)                                                                       friends#edit
                                  friend GET    /friends/:id(.:format)                                                                            friends#show
                                         PATCH  /friends/:id(.:format)                                                                            friends#update
                                         PUT    /friends/:id(.:format)                                                                            friends#update
                                         DELETE /friends/:id(.:format)                                                                            friends#destroy
                              home_about GET    /home/about(.:format)                                                                             home#about
                                    root GET    /                                                                                                 home#index
        turbo_recede_historical_location GET    /recede_historical_location(.:format)                                                             turbo/native/navigation#recede
        turbo_resume_historical_location GET    /resume_historical_location(.:format)                                                             turbo/native/navigation#resume
       turbo_refresh_historical_location GET    /refresh_historical_location(.:format)                                                            turbo/native/navigation#refresh
           rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                           action_mailbox/ingresses/postmark/inbound_emails#create
              rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                              action_mailbox/ingresses/relay/inbound_emails#create
           rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                           action_mailbox/ingresses/sendgrid/inbound_emails#create
     rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#health_check
           rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#create
            rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                                       action_mailbox/ingresses/mailgun/inbound_emails#create
          rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#index
                                         POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#create
       new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                                      rails/conductor/action_mailbox/inbound_emails#new
      edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                                 rails/conductor/action_mailbox/inbound_emails#edit
           rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#show
                                         PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#update
                                         PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#update
                                         DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET    /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format)                              rails/conductor/action_mailbox/inbound_emails/sources#new
   rails_conductor_inbound_email_sources POST   /rails/conductor/action_mailbox/inbound_emails/sources(.:format)                                  rails/conductor/action_mailbox/inbound_emails/sources#create
   rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                               rails/conductor/action_mailbox/reroutes#create
rails_conductor_inbound_email_incinerate POST   /rails/conductor/action_mailbox/:inbound_email_id/incinerate(.:format)                            rails/conductor/action_mailbox/incinerates#create
                      rails_service_blob GET    /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format)                               active_storage/blobs/redirect#show
                rails_service_blob_proxy GET    /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format)                                  active_storage/blobs/proxy#show
                                         GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                                        active_storage/blobs/redirect#show
               rails_blob_representation GET    /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
         rails_blob_representation_proxy GET    /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)    active_storage/representations/proxy#show
                                         GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)          active_storage/representations/redirect#show
                      rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                                       active_storage/disk#show
               update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                               active_storage/disk#update
                    rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)  

    标签: ruby-on-rails ruby devise


    【解决方案1】:

    对于直到(不包括)7 的 rails 版本,method: :delete 就足够了。我在一个新的 rails 7 项目中遇到了同样的问题,现在看来你必须使用data: {turbo_method: :delete}。其他非 GET 请求也是如此,尽管您也可以使用 button_to 之类的东西作弊,默认为 POST 请求。

    例如:

    <%= link_to "Log out", destroy_user_session_path, data: { turbo_method: :delete }, class: "some-class" %>
    

    【讨论】:

    • 你好,非常感谢你的评论。我添加了 data: {turbo_method: :delete} 代码,但它仍然不起作用。如果我手动将地址更改为 users/sign_out,我会不断收到与以前相同的错误消息:路由错误,没有路由匹配 [GET] "/users/sign_out"
    • 我认为我们需要更多信息,因为这应该开箱即用。你在使用 Rails 7 吗?你有涡轮导轨和热线吗?您的路线是如何创建的?
    【解决方案2】:

    似乎您可能错过了使魔术发生的 JavaScript。你确定一切都包括在内? Turbo 是一个 JavaScript 库,它可以将链接变成一个帖子。这并不令人困惑或任何东西:D

    https://turbo.hotwired.dev/handbook/drive#performing-visits-with-a-different-method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 2014-09-15
      • 2011-02-15
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多