禁用GET for Devise路由,Rails 5

时间:2017-10-13 20:01:38

标签: ruby-on-rails ruby devise ruby-on-rails-5

我有使用Devise进行Ajax登录/注册的Rails 5,我想删除这两个动作的GET请求。默认的sign_in / sign_up路由已更改。这是我的routes.rb

devise_for :users, :path => '', :path_names => { :sign_in => "login", 
                       :sign_out => "logout", :sign_up => "registration" }, 
                       :controllers => {:sessions => 'sessions', 
                                        :registrations => 'registrations'

2 个答案:

答案 0 :(得分:1)

<div class="navigation"> <div id="productions"> <a href="./productions.html">Mes productions</a> </div> <a href="../DJ/DJ.html">DJ</a> <a target="_blank" href="../CV.pdf">Mon CV</a> <a href="../contact/contact.html">Me contacter</a> </div> .navigation { padding: 40px 0px; position: relative; text-align: center; width: 100%; font-size: 30px; } .navigation a { background: black; border: 1px solid grey; border-radius: 7px; color: white; display: inline-block; margin: 100px 35px 0px 35px; padding: 14px; text-decoration: none; opacity: 0.75; font-family: impact; } .navigation a:hover { background: white; border: 1px solid black; color: black; } #productions a { color: black !important; background: white !important; } sessions_controller.rb中,您可以检查请求类型,如果是registrations_controller.rb请求则返回404:

如果您尚未对设备控制器进行猴子修补,请创建目录GET并将文件app/controllers/devise添加到其中:

registrations_controller.rb

sessions_controller也是如此。您可以通过禁用所有class Devise::RegistrationsController < DeviseController prepend_before_action: :check_get_request # you can limit it to certain actions with only: [:new, etc.] private def check_get_request if request.get? # respond with 404 or 422, or whatever else super end end end 请求来解决问题,但如果需要,您可以定位特定操作:供参考:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb 并且:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb

答案 1 :(得分:0)

因为我使用的是自定义registration_controllersessions_controller,所以我只是覆盖显示注册/登录页面的方法。两种方法都是show。我向这两个自定义控制器添加了这个方法:

def new
  raise ActionController::RoutingError.new('Not Found')
end

如果有人导航到注册或登录URL,则返回404,但POST请求正常。