Ruby on rails Rails 404重定向方法影响控制器操作

Ruby on rails Rails 404重定向方法影响控制器操作,ruby-on-rails,Ruby On Rails,我有一个rails应用程序,如果用户键入的url不存在,我将重定向到404页面。我这样做是因为我的日志显示有人试图在我的代码中找到漏洞来攻击该网站。不幸的是,强制重定向的代码正在影响我无法订阅个人列表的应用程序。这是我的application_controller.rb文件 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # Fo

我有一个rails应用程序,如果用户键入的url不存在,我将重定向到404页面。我这样做是因为我的日志显示有人试图在我的代码中找到漏洞来攻击该网站。不幸的是,强制重定向的代码正在影响我无法订阅个人列表的应用程序。这是我的application_controller.rb文件

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :verified_request?
  $attack_attempts = 0



  rescue_from ActiveRecord::RecordNotFound, with: :not_found 
  rescue_from Exception, with: :not_found
  rescue_from ActionController::RoutingError, with: :not_found


  def verified_request?
    if request.content_type == "application/json"
      true
    else
      super()
    end
  end   


  def raise_not_found     
     raise ActionController::RoutingError.new("No route matches # {params[:unmatched_route]}")
  end

 private

  def not_found
    $attack_attempts+=1
    if($attack_attempts >=5)
        flash[:alert] = ' Your attempt to hack this website has been recorded. Stop doing this, or else you would be fined. You wont like it.'
    end  
    respond_to do |format|
       format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
       format.xml { head :not_found }
       format.any { head :not_found }
    end
  end
  def error
     respond_to do |format|
         format.html { render :file => "#{Rails.root}/public/500", :layout => false, :status => :error }
         format.xml { head :not_found }
         format.any { head :not_found }
     end
  end   
end
为了进一步参考,如果未能执行,则此操作为:

def subscribe
    begin
    gb = Gibbon::Request.new
    list_id = "********" list id hidden for security sake.
    subscribe = gb.lists(list_id).members.create(body: {email_address: params[:email], status: "subscribed", merge_fields: {FNAME: params[:first_name], LNAME: params[:last_name]}})
        if subscribe
            redirect_to :back, notice: " Congrats You'll be notified as soon as we go live!!"
        end
    rescue Gibbon::MailChimpError => e
        redirect_to :back, alert: " Please fill out all fields with the required info to subscribe, emails in this format:abcd..1234@youremailprovider.youremailprovider'sdomain" 
    end 
end 
正如@jvnill所问的那样。我正在添加routes.rb文件:

Rails.application.routes.draw do

  root 'openhwy#coming_soon'

  post 'subscribe', to: 'openhwy#subscribe'

  get 'about_us', to: 'openhwy#about_us'

  get 'pricing_page', to: 'openhwy#pricing_page'

  get 'contact/send_contact_request'

  post 'contact', to: 'openhwy#contact'

  get '*unmatched_route', to: 'application#raise_not_found'

end
回答:

感谢@jvnill和@marek的贡献。此行出现错误:

rescue_from Exception, with: :not_found

这就迫使404页面在任何时候出现错误,甚至其中一个错误与该操作无关。

我感觉您在订阅控制器操作中遇到了一些异常,因为行:

rescue_from Exception, with: :not_found
在ApplicationController中,您的应用程序正在呈现404


Marek

您是否将“全包”路线放在路线文件的底部?是的。我会用routes文件更新这个问题@我的坏朋友。从日志中可以看出,请求并没有通过catch-all路由。它仍然会转到
subscribe
操作。我认为问题在于gb.lists(list\u id)。如果您使用的是lists方法中的
.find
,而您传递的id不在数据库中,那么您将在生产中获得404。@jvnill在我删除错误方法的那一刻。它很好用。在开发过程中,我没有设置api键,这就是当自定义404重定向方法不存在时我得到的错误。当我把它们加回去的时候,这种情况就发生了。另一个原因可能是因为
重定向到:back
你在那里的电话。在使用redirect_to:back时,从文档
中,如果没有引用者,将引发ActionController::RedirectBackError。您可以通过拯救ActionController::RedirectBackError为这种情况指定一些回退行为。
我删除了它,并且它在开发中起作用。我会转向生产,看看它是否有效。谢谢你的贡献。哦,太好了+我没看到。
rescue_from Exception, with: :not_found