为什么我得到"找不到错误404"在Ruby on Rails中?

时间:2016-08-16 17:47:27

标签: ruby-on-rails ruby heroku

这是我的问题:当我运行命令rails server并转到localhost:3000时,我得到页面"找不到错误404",所以我尝试将localhost:3000 / home因为这是一个观点,但问题还在继续。这是CMD中的日志..

C:\Sites\ifurniture>rails s
=> Booting WEBrick
=> Rails 4.2.7.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-08-16 12:45:45] INFO  WEBrick 1.3.1
[2016-08-16 12:45:45] INFO  ruby 2.2.4 (2015-12-16) [i386-mingw32]
[2016-08-16 12:45:45] INFO  WEBrick::HTTPServer#start: pid=192 port=3000


Started GET "/" for ::1 at 2016-08-16 12:45:47 -0500
  ActiveRecord::SchemaMigration Load (1.0ms)  SELECT "schema_migrations".* FROM
"schema_migrations"
Processing by Refinery::PagesController#home as HTML
  Parameters: {"locale"=>:es}
  Refinery::Page Load (1.0ms)  SELECT  "refinery_pages".* FROM "refinery_pages"
WHERE "refinery_pages"."link_url" = ? LIMIT 1  [["link_url", "/"]]
  Refinery::Page Load (7.0ms)  SELECT  "refinery_pages".* FROM "refinery_pages"
WHERE "refinery_pages"."menu_match" = ?  ORDER BY "refinery_pages"."id" ASC LIMI
T 1  [["menu_match", "^/404$"]]
  Rendered public/404.html (14.0ms)
Filter chain halted as :find_page rendered or redirected
Completed 404 Not Found in 1974ms (Views: 1775.1ms | ActiveRecord: 11.0ms)


Started GET "/home" for ::1 at 2016-08-16 12:46:00 -0500
Processing by Refinery::PagesController#show as HTML
  Parameters: {"path"=>"home", "locale"=>:es}
  Refinery::Page Load (29.0ms)  SELECT  "refinery_pages".* FROM "refinery_pages"
 INNER JOIN "refinery_page_translations" ON "refinery_page_translations"."refine
ry_page_id" = "refinery_pages"."id" WHERE "refinery_pages"."parent_id" IS NULL A
ND "refinery_page_translations"."locale" IN ('es', 'en') AND "refinery_page_tran
slations"."slug" = 'home'  ORDER BY "refinery_pages"."id" ASC LIMIT 1
  Refinery::Page Load (0.0ms)  SELECT  "refinery_pages".* FROM "refinery_pages"
WHERE "refinery_pages"."menu_match" = ?  ORDER BY "refinery_pages"."id" ASC LIMI
T 1  [["menu_match", "^/404$"]]
  Rendered public/404.html (0.0ms)
Filter chain halted as :find_page rendered or redirected
Completed 404 Not Found in 1475ms (Views: 1207.8ms | ActiveRecord: 29.0ms)
[2016-08-16 12:46:07] INFO  going to shutdown ...
[2016-08-16 12:46:07] INFO  WEBrick::HTTPServer#start done.
Exiting

这是我的路线文件..

Rails.application.routes.draw do
  # This line mounts Refinery's routes at the root of your application.
  # This means, any requests to the root URL of your application will go to Refinery::PagesController#home.
  # If you would like to change where this extension is mounted, simply change the
  # configuration option `mounted_path` to something different in config/initializers/refinery/core.rb
  #
  # We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"
  post '/suscribir' => 'subscribe#create'
  mount Refinery::Core::Engine, at: Refinery::Core.mounted_path

end

pages_Controller.rb代码..

module Refinery
  class PagesController < ::ApplicationController
    include Pages::RenderOptions
    include Productos

    before_action :find_page, :set_canonical
    before_action :error_404, :unless => :current_user_can_view_page?

    # Save whole Page after delivery
    after_action :write_cache?

    # This action is usually accessed with the root path, normally '/'
    def home
      @posts = Blog::Post.newest_first.live.includes(:comments, :categories)
      render_with_templates?
    end

    # This action can be accessed normally, or as nested pages.
    # Assuming a page named "mission" that is a child of "about",
    # you can access the pages with the following URLs:
    #
    #   GET /pages/about
    #   GET /about
    #
    #   GET /pages/mission
    #   GET /about/mission
    #
    def show
      @productos = Refinery::Productos::Producto.all
      if should_skip_to_first_child?
        redirect_to refinery.url_for(first_live_child.url) and return
      elsif page.link_url.present?
        redirect_to page.link_url and return
      elsif should_redirect_to_friendly_url?
        redirect_to refinery.url_for(page.url), :status => 301 and return
      end

      render_with_templates?
    end

  protected

    def requested_friendly_id
      if ::Refinery::Pages.scope_slug_by_parent
        # Pick out last path component, or id if present
        "#{params[:path]}/#{params[:id]}".split('/').last
      else
        # Remove leading and trailing slashes in path, but leave internal
        # ones for global slug scoping
        params[:path].to_s.gsub(%r{\A/+}, '').presence || params[:id]
      end
    end

    def should_skip_to_first_child?
      page.skip_to_first_child && first_live_child
    end

    def should_redirect_to_friendly_url?
      requested_friendly_id != page.friendly_id || (
        ::Refinery::Pages.scope_slug_by_parent &&
        params[:path].present? && params[:path].match(page.root.slug).nil?
      )
    end

    def current_user_can_view_page?
      page.live? || authorisation_manager.allow?(:plugin, "refinery_pages")
    end

    def first_live_child
      page.children.order('lft ASC').live.first
    end

    def find_page(fallback_to_404 = true)
      @page ||= case action_name
                when "home"
                  Refinery::Page.find_by(link_url: '/')
                when "show"
                  Refinery::Page.friendly.find_by_path_or_id(params[:path], params[:id])
                end
      @page || (error_404 if fallback_to_404)
    end

    alias_method :page, :find_page

    def set_canonical
      @canonical = refinery.url_for @page.canonical if @page.present?
    end

    def write_cache?
      # Don't cache the page with the site bar showing.
      if Refinery::Pages.cache_pages_full && !authorisation_manager.allow?(:read, :site_bar)
        cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path).to_s)
      end
    end
  end
end

当我尝试将所有内容部署到Heroku时,我的应用程序崩溃了,我得到了#34;应用程序错误&#34; ....请帮助我,我是Ruby on Rails的新手,谢谢。

3 个答案:

答案 0 :(得分:1)

@iFurniture,问题在这里,你的&#39; fallback_to_404&#39;设置为true, 所以你总是得到error_404

def find_page(fallback_to_404 = true)
  @page ||= ...
  @page || (error_404 if fallback_to_404)
end

我建议你这样做:

def find_page(fallback_to_404: false)
  error_404 if fallback_to_404
  @page ||= ...
  @page || (error_404 if fallback_to_404)
end

因此默认情况下fallback_to_404将为false,如果您想将其设置为true,请将其称为

find_page(fallback_to_404: true)

Ruby 2 Keyword Arguments

关于这个:

@page || (error_404 if fallback_to_404)

你可能想要吗?

fallback_to_404 ? error_404 : @page

看起来更好。

答案 1 :(得分:0)

我会检查你的before_action :error_404, :unless => :current_user_can_view_page?或许暂时删除该代码以查看它是否可以加载,如果有,那么很可能该方法设置不正确。

答案 2 :(得分:0)

我可以解决运行CMD的问题

bundle exec rake db:seed

感谢您的帮助。