0

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).

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?

2 Answers2

2

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?

alf
  • 18,372
  • 10
  • 61
  • 92
1
def my_action
 respond_to do |format|
   format.html { render 'my_unified_view' }
 end
cpuguy83
  • 5,794
  • 4
  • 18
  • 24
  • Thanks, but is there a more DRY way to do this? – user1539664 Jul 21 '12 at 14:13
  • You could put the respond_to block in a method in ApplicationController and just call it at the end of your actions. I'm sure there is something you could monkey patch to also render your view instead of the default action.html template, but that seems like it's going a bit too far. – cpuguy83 Jul 21 '12 at 14:51