match 'url' => 'controller#method', :constraints => { :only_ajax => true }
class BooksController < ApplicationController
before_action :access_check, only:[:new]
def new
@book = Book.new
form_html = render_to_string( :partial => 'books/form', :formats => [:html], :locals => { :book => @book } )
render :json => { :form_html => form_html }
end
private
def access_check
unless request.xhr?
redirect_to :root_path
end
end
end