在 Ruby on Rails 中设置自定义 mimetypes 的视图格式?
我正在rails中创建一个自定义mimetype以与respond_to一起使用
Mime::Type.register_alias "text/html", :modal
我想在respond_to中使用此mime类型,如下所示:
respond_to do |format|
format.html{ render 'index'}
format.modal{ render 'index', :layout => 'bare'}
end
我想要这种格式基本上提供与 .thml 格式相同的视图,但布局不同,
我收到丢失模板错误
Missing template support/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:modal], :locale=>[:en, :en]} 在视图路径“/remote/app/views”中,
它正在寻找名为 index.modal.erb
的视图 我目前只有 index.html.erb
我尝试创建 index.modal.erb
并且它确实有效,但布局有同样的问题我只有一个布局在 bare.html.erb
我真的不想为不同的 mime 类型复制这些视图文件。我希望有一种方法可以让自定义 mime 类型回到我所缺少的 html 视图上。
I'm creating a custom mimetype in rails to use with respond_to
Mime::Type.register_alias "text/html", :modal
I want to use this mime type in respond_to like so:
respond_to do |format|
format.html{ render 'index'}
format.modal{ render 'index', :layout => 'bare'}
end
I want this format to basically serve the same views as the .thml format but with a different layout
I'm getting a missing template error
Missing template support/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:modal], :locale=>[:en, :en]} in view paths "/remote/app/views",
its looking for a view named index.modal.erb
i currently only have index.html.erb
I've tried creating the index.modal.erb
and it does work but then the layout has the same problem i only have a layout at bare.html.erb
I really don't want to duplicate these view files for the different mime types. I'm hoping theres a way to have the custom mime type fall back on to html views that i am missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将按照你的要求
我有一种应该在 Rails 3 中工作的不同方法(在 Rails 3.2.12 中测试)。将以下内容放入控制器中:
摘要:
这将完全满足您的需要,即与 .html 相同的操作,但具有不同的布局。
I am going by your requirement that
I have a different approach that should work in Rails 3 (tested in Rails 3.2.12). Put the following in your controller:
Summary:
This will do exactly what you need, i.e., same action as .html but with a different layout.
您使用什么版本的 Rails?如果是 3.2,请尝试为模态响应显式设置格式:
这应该使其呈现index.html.erb 而不是index.modal.erb。
What version of Rails are you using? If 3.2, try setting the format explicitly for your modal response:
That should make it render index.html.erb instead of index.modal.erb.