在 Ruby on Rails 中设置自定义 mimetypes 的视图格式?

发布于 2024-12-27 18:40:46 字数 1148 浏览 1 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2025-01-03 18:40:46

我将按照你的要求

我希望这种格式基本上提供与 .html 格式相同的视图,但布局不同

我有一种应该在 Rails 3 中工作的不同方法(在 Rails 3.2.12 中测试)。将以下内容放入控制器中:

  before_filter do
    @bare= (params[:format] == 'modal')

    if @bare
      params[:format]= 'html'
      request.format= :html
    end
  end

  layout :select_layout

  # standard controller stuff

  # ...
  # Towards bottom of your controller code, 
  private
  def select_layout
    @bare ? 'bare' : nil
  end

摘要:

这将完全满足您的需要,即与 .html 相同的操作,但具有不同的布局。

I am going by your requirement that

I want this format to basically serve the same views as the .html format but with a different layout

I have a different approach that should work in Rails 3 (tested in Rails 3.2.12). Put the following in your controller:

  before_filter do
    @bare= (params[:format] == 'modal')

    if @bare
      params[:format]= 'html'
      request.format= :html
    end
  end

  layout :select_layout

  # standard controller stuff

  # ...
  # Towards bottom of your controller code, 
  private
  def select_layout
    @bare ? 'bare' : nil
  end

Summary:

  • Change the format back to html.
  • Set a variable to indicate that it needs to serve 'bare'
  • Tell controller that value of layout will be given by a function. (See 'Choosing Layouts at Runtime' in http://guides.rubyonrails.org/layouts_and_rendering.html)
  • No changes needed in any of the respond_to blocks.

This will do exactly what you need, i.e., same action as .html but with a different layout.

戏舞 2025-01-03 18:40:46

您使用什么版本的 Rails?如果是 3.2,请尝试为模态响应显式设置格式:

respond_to do |format|                                                                                                                                     
  format.html  { render 'index'}                                                                                                                             
  format.modal { render 'index', :formats => [:html], :layout => 'bare'}                                                                                      
end

这应该使其呈现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:

respond_to do |format|                                                                                                                                     
  format.html  { render 'index'}                                                                                                                             
  format.modal { render 'index', :formats => [:html], :layout => 'bare'}                                                                                      
end

That should make it render index.html.erb instead of index.modal.erb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文