失败/错误:引发ActionController :: RoutingError,“没有路由匹配[#{env ['REQUEST_METHOD']}]#{env ['PATH_INFO']。inspect}”

时间:2018-09-22 22:22:44

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

使用Rails 5.2.1,

我正在为我的应用添加自定义的可能性,该应用基本上是博客...

该应用程序的着陆页可以自定义,博客所有者可以更改图像,颜色等...

因此,我为每个可以个性化的图像创建了一个自定义控制器,模型以及上载器。 自定义只有一个实例。

所以就本地而言,所有事物都像魅力一样。

Rspec失败,因为我正在登录页面中调用@customization ... 无法在 Article

中添加或编辑图像

rspec失败

Failures:

  1) managing article The admin is logged in edit a article text
     Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"

     ActionController::RoutingError:
       No route matches [GET] "/images/fallback/default.png"
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:38:in `call_app'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `block in call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `block in tagged'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:28:in `tagged'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `tagged'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/request_id.rb:27:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/static.rb:127:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:524:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/urlmap.rb:68:in `block in call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `each'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/capybara-3.7.2/lib/capybara/server/middleware.rb:48:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'
     #
     #   Showing full backtrace because every line was filtered out.
     #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
     #   RSpec::Configuration#backtrace_inclusion_patterns for more information.
     # ------------------
     # --- Caused by: ---
     # Capybara::CapybaraError:
     #   Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true
     #   /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/capybara-3.7.2/lib/capybara/session.rb:144:in `raise_server_error!'

  2) managing article The admin is logged in add images for an article
     Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"

     ActionController::RoutingError:
       No route matches [GET] "/images/fallback/default.png"
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:38:in `call_app'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `block in call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `block in tagged'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:28:in `tagged'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `tagged'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/request_id.rb:27:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/static.rb:127:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:524:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/urlmap.rb:68:in `block in call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `each'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rack-2.0.5/lib/rack/urlmap.rb:53:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/capybara-3.7.2/lib/capybara/server/middleware.rb:48:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
     # /Users/bill/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'
     #
     #   Showing full backtrace because every line was filtered out.
     #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
     #   RSpec::Configuration#backtrace_inclusion_patterns for more information.
     # ------------------
     # --- Caused by: ---
     # Capybara::ElementNotFound:
     #   Unable to find visible css "#label_uploader_2"
     #   ./spec/features/managing_article_spec.rb:51:in `block (3 levels) in <top (required)>'

customization_controller.rb

class CustomizationController < ApplicationController
    before_action  :authorize

    def show
        @customization = Customization.instance
    end

    def update
        @customization = Customization.instance
        if @customization.update(customization_params)
            redirect_to customization_path, notice: "Successfully updated"
        end
    end

    private

    def customization_params
        params.require(:customization).permit( 
            :main_image, 
            :first_section_image,
            :second_section_image,
            :third_section_image,
            :menu_color,
            :body_color,
            :icon_color,
            :about,
            :avatar
            )
    end
end

customization.rb

class Customization < ApplicationRecord
    mount_uploader :avatar,               AvatarUploader
    mount_uploader :main_image,           MainImageUploader
    mount_uploader :first_section_image,  FirstSectionImageUploader
    mount_uploader :second_section_image, SecondSectionImageUploader
    mount_uploader :third_section_image,  ThirdSectionImageUploader

    INSTANCE_ID =  87

    def self.instance
        Customization.find_or_create_by(id: INSTANCE_ID)
    end

end

上传者

class ThirdSectionImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

  if Rails.env.production?
    storage :fog
  else
    storage :file
  end

  version :thumb do
     process resize_to_fill: [280, 280]
   end

  def default_url(*args)
     "/images/fallback/" + [version_name, "default.png"].compact.join('_')
   end


  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

home_controller.rb

class HomeController < ApplicationController

  def index
    @customization = Customization.instance
  end
end

home / index.html.rb-aka登陆页面

<%= link_to videos_path do %>
    <figure class="figure">
        <%= image_tag(@customization.third_section_image_url.present? ? @customization.third_section_image_url : "https://s3-eu-west-1.amazonaws.com/beehappy/images_site/bibi.jpg",  class: 'figure-img img-fluid') %>
      <figcaption class="figure-caption text-gold">Media</figcaption>
    </figure>
<% end %>

更新

articles_spec.rb

require "rails_helper"

RSpec.feature "managing articles" do 

    fixtures :articles, :user

    context "The admin is logged in" do 

        before :each do
            login_as :admin
        end

        scenario "edit an article text", :js do 
            guitar = articles(:fender)
            visit article_path(guitar)

            find(".edit_link").click

            fill_in "article[title]", with: "Fender Telecaster"

            click_on "Update Me"

            expect(page).to have_content("Article was successfully updated")
        end

        scenario "add images for a guitar" do
            guitar = articles(:gibson)
            visit article_path(guitar)

            find(".edit_link").click

            click_on "Add pictures"

            within "#label_uploader_2" do 
                within "#span_uploader_2" do 
                    attach_file("article[attachments][]" , Rails.root.join("spec/assets/images/img_2.jpg"))
                end
            end

            click_on "Update"

            expect(page).to have_content("Successfully added")
        end
    end
end

1 个答案:

答案 0 :(得分:1)

实际错误是No route matches [GET] "/images/fallback/default.png"。您正在载波上传器上设置默认路径,但看起来图像丢失。那张照片在哪里?该路线应该在/public/images/fallback/default.png