-
-
Save chrise86/4810a223f771dfff0f4b8662396d7dec to your computer and use it in GitHub Desktop.
Render a complete page in rails 5 without controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create the template | |
template = PageOfflineTemplate.new | |
template.quote = quote | |
template.pages = quote.build_pages | |
# Here I render a template with layout to a string then a PDF | |
pdf = PDFKit.new template.render_to_string(:template=>"quotes/review.html.haml") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OfflineTemplate < AbstractController::Base | |
include AbstractController::Logger | |
include AbstractController::Rendering | |
include ActionView::Layouts | |
include AbstractController::Helpers | |
include AbstractController::Translation | |
include AbstractController::AssetPaths | |
include ActionDispatch::Routing::UrlFor | |
include Rails.application.routes.url_helpers | |
Rails.application.routes.default_url_options = Rails.application.config.action_mailer.default_url_options | |
helper ApplicationHelper | |
helper_method :protect_against_forgery? | |
# configure the different paths correctly | |
def initialize(*_args) | |
super | |
lookup_context.view_paths = Rails.root.join('app', 'views') | |
config.javascripts_dir = Rails.root.join('public', 'javascripts') | |
config.stylesheets_dir = Rails.root.join('public', 'stylesheets') | |
config.assets_dir = Rails.root.join('public') | |
end | |
# we are not in a browser, no need for this | |
def protect_against_forgery? | |
false | |
end | |
# so that your flash calls still work | |
def flash | |
{} | |
end | |
def params | |
{} | |
end | |
def controller_name | |
'offline' | |
end | |
# same asset host as the controllers | |
self.asset_host = ActionController::Base.asset_host | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# subclass to define the @ attributes your tempalte will use | |
class PageOfflineTemplate < OfflineTemplate | |
attr_accessor :pages, :quote, :invoice, :quotes, :mat_pages, :wo_pages, :exp_pages | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment