我如何举起然后捕捉并刷新此错误的轨道

时间:2018-08-23 21:24:30

标签: ruby-on-rails

在user_documents_controller.rb中,您是说

        //Filter out colon cleanse duo
    $items = array_filter($order->get_items(), function($item) {
        return $item->get_product_id() != 3404;
    });

    //Randomize ids and grab one
    shuffle($items);
    foreach( $items as $item) :

        $product_id = $item->get_product_id();
        $product_name = $item->get_name();

        break;
    endforeach;

但我收到此错误class YourController < ApplicationController before_action :set_document def download file = @document.external_document if file.nil? render 'the_view_you_called_this_action', flash[:error]: 'Document not found' # Or probably you need to use flash.now[:error] = 'Document not found' return end # The rest of your code end end

但是,如果我这样做的话:

ActionController::UnknownFormat at /documents/ee3ej2e2oijwejdowj/download UserDocumentsController#download is missing a template for this request format and variant. request.formats: ["text/html"] request.variant:

我收到以下错误消息:如果文档不存在,则在# Downloads a document from the Java-Backend and sends it to the client def download if file = @document.external_document original_filename = file.headers['content-disposition'].partition('filename=').last original_filetype = File.extname(original_filename) send_data file, filename: "#{@document.meta.title}#{original_filetype}", type: file.headers['content-type'] else if file.nil? redirect_to user_document_path, flash.now[:error] = 'Document not found' end end end 行中的undefined method 'partition' for nil:NilClass,如果存在,则可以下载该文档。

1 个答案:

答案 0 :(得分:1)

对代码进行很多 的假设,您可以执行以下操作:

# In your controller
class YourController < ApplicationController
  before_action :set_document

  def download
    file = @document.external_document
    if file.nil?
      render 'the_view_you_called_this_action', flash[:error]: 'Document not found' # Or probably you need to use flash.now[:error] = 'Document not found'
      return
    end
    # The rest of your code
  end
end