我想创建标题中指定的自定义渲染。
例如,我有我的 Controller :
class MyController < ApplicationController
def index
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @objs }
end
end
end
,但我想要这样的东西:
class MyController < ApplicationController
def index
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @objs }
format.my_format { render :my_format => @objs }
end
end
end
这可能吗?我需要采取哪些步骤才能使其发挥作用?
提前致谢!
更新
我想要类似 here 的内容。所以我用一个方法替换了@objs,但它也不起作用(该方法没有被调用)。
观察:我在 config/initializers/mime_types.rb 中注册了 mime 类型。
最佳答案
来自railsapi.com :
"If you need to use a MIME type which isn’t supported by default, you can register your own handlers in environment.rb as follows."
Mime::Type.register "image/jpg", :jpg
关于ruby-on-rails - 如何覆盖渲染以在 Rails 2.3(.5) 中创建自定义 "render :my_format => argument"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2630284/