导轨|如果格式不匹配重定向到404

时间:2017-04-29 08:11:51

标签: ruby-on-rails redirect controller format

我有一个rails应用程序和联系页面。所以链接是www.mypage.com/contact。当某人尝试/contact.php或/contact.aspx时,我得到500错误。格式未知。

有没有办法将它们重定向到404页?

修改

但问题不在于未找到的记录。这就是我得到的;

An ActionView::MissingTemplate occurred in main#contact:

Missing template main/contact, application/contact with {:locale=>[:tr], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/app/app/views"
* "/app/vendor/bundle/ruby/2.2.0/gems/mailboxer-0.13.0/app/views"

2 个答案:

答案 0 :(得分:1)

将以下行添加到application_controller.rb

  rescue_from ActiveRecord::RecordNotFound do
    flash[:warning] = 'Resource not found.'
    redirect_back_or root_path
  end

答案 1 :(得分:0)

如果您要为ActionController::UnknownFormat错误呈现404错误,则需要向application_controller.rb添加类似的内容:

rescue_from ActionController::UnknownFormat do |e|
    request.format = :html
    redirect_404
end

def redirect_404
    raise ActionController::RoutingError.new('Requested format not available.')
end