【发布时间】:2014-02-26 19:58:05
【问题描述】:
当我尝试让自定义输出与 respond_to/with 一起表现时,我得到一个 ActionView::MissingTemplate,就像它表现出 xml/json 输出一样。
我的对象上有一个可用的自定义输出格式。
@item.to_custom
我已使用 mime_types.rb 中注册的自定义 Mime::Type 注册格式。
Mime::Type.register "application/custom", :custom
我已将其列在控制器的 respond_to 选项中
class ItemsController < ApplicationController
respond_to :html, :xml, :custom
然后在我的表演动作结束之前我有一个 respond_with 方法。
def show
@item = Item.find(params[:id])
respond_with(@item)
end
当我访问 items/1234.xml 时,我得到了 xml 输出。当我尝试访问 items/1234.custom 时,我收到错误 ActionView::MissingTemplate。我可以通过添加文件 app/views/show.custom.ruby 的内容来修复它:
@item.to_custom
有没有办法让 to_custom 在 response_to/with 设置中像 to_xml 或 to_json 一样工作?只使用 to_custom 方法而不需要模板?还是我必须使用显式调用该方法的视图模板?
【问题讨论】:
标签: ruby-on-rails ruby