使用布局响应所有HTML请求 [英] Respond to all HTML requests with layout

查看:87
本文介绍了使用布局响应所有HTML请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Rails应用程序,该应用程序将使用具有自己的路由功能的客户端框架。我想使用pushState路由,因此需要配置Rails路由器以响应这样的请求(足够容易)。

I'm working on a Rails app that will be using a client-side framework with its own routing feature. I'd like to use pushState routing, so the Rails router will need to be configured to respond to such requests (easy enough).

有一种简单的设置方法所有具有有效路径的HTML请求都可以通过布局进行响应,而不必在我的views文件夹中堆放一堆空白的 action.html.erb 文件?

Is there an easy way to set all HTML requests with a valid route to be responded to with just a layout, without having to clutter up my views folder with a bunch of blank action.html.erb files?

推荐答案

这是一种拦截对有效路由的请求并为每个非ajax请求呈现视图的方法:

Here's a way to intercept requests to valid routes and render a view for every non-ajax request:

app / controllers / application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :render_default_view

  # ...

  private

  def render_default_view
    return if request.xhr?
    respond_to do |format|
      format.html { render 'public/default_view.html', :layout => nil }
    end
  end
end

我认为你想要什么,对吧?

I think this does what you want, right?

这篇关于使用布局响应所有HTML请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆