【问题标题】:ActionController::UnknownFormat rails 5 controllerActionController::UnknownFormat rails 5 控制器
【发布时间】:2018-08-21 14:14:34
【问题描述】:

编辑#1

查看订单/显示,我还没有添加发票,但视图应该可以使用或不使用发票链接,对吧?

<section class="pt-4 px-8 animated">
  <section class="flex flex-wrap justify-between">
    <h3 class="font-normal text-grey-dark mb-4 py-2">Order Information</h3>
    <%= render 'layouts/dashboard/account' %>
  </section>

  <h2 class="text-grey-darkest text-lg no-underline py-4">
    Course: <%= @order.course.title %>
  </h2>

  <section class="py-2">
    <p class="text-grey-darkest text-md pb-4"><%= @order.course.description %></p>

  <p class="text-grey-darkest text-md">
    <b>Course Price:</b>
    £<%= @order.course.price %>
  </p>

  <p class="text-grey-darkest text-md">
    <b>Total Price:</b>
    £<%= @order.amount %>
  </p>
  </section>

  <section class="text-grey-darkest pt-4">
    <h4 class="pb-2">User details</h4>
    <p>Name: <%= current_user.full_name %></p>
    <p>Email: <%= current_user.email %></p>
  </section>
</section>

原始问题

我的控制器出现ActionController::UnknownFormat 错误

class OrdersController < ApplicationController
  layout proc { user_signed_in? ? "dashboard" : "application" }

  before_action :authenticate_user!

  def index
    @orders = Order.includes(:course).all
  end

  def show
    @order = Order.find(params[:id])

    respond_to do |format|
      format.pdf {
        send_data @order.receipt.render,
        filename: "#{@order.created_at.strftime("%Y-%m-%d")}-aurameir-courses-receipt.pdf",
        type: "application/pdf",
        disposition: :inline
      }
    end
  end

  def create
  end

  def destroy
  end
end

我正在使用 https://github.com/excid3/receipts gem,它使用 prawn,但我不确定为什么会出现错误。

【问题讨论】:

  • 您能否提供包含显示操作链接的视图?
  • 查看编辑#1 @IgorDrozdov
  • 所以您尝试在浏览器中输入网址然后收到此错误消息?
  • 所以我要去订单localhost:3000/orders/1 并得到错误

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


【解决方案1】:

由于html 格式是Rails 中的默认格式,您需要在您的网址或链接生成器中明确提供pdf 格式:

localhost:3000/orders/1.pdf

否则,它会尝试将其加载为 localhost:3000/orders/1.html,但您的控制器中尚未定义 format.html 处理程序。

当您在应用程序中实现链接时,请按照docs 中的示例进行操作:

<%= link_to "Download Receipt", charge_path(@charge, format: :pdf) %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多