PDFKit Middleware doesn't work - ruby-on-rails

I've read that adding the extension ".pdf" to the URL of my application I can generate a pdf file using the PDFKit Middleware but I can't make it work.
My application.rb:
require_relative 'boot'
require 'rails/all'
require 'pdfkit'
Bundler.require(*Rails.groups)
module Ifo
class Application < Rails::Application
config.i18n.available_locales = [:en, :it]
config.i18n.default_locale = :it
config.active_record.schema_format = :sql
config.middleware.use PDFKit::Middleware
end
end
as stated in PDFKit's documentation, but this makes rails crash.
(using PDFKit 0.8.2 in Rails 5)

I think can be one of this two:
1) Note than in application.rb, where to put each of your lines
require 'pdfkit'
module YouApp
class Application < Rails::Application
....
config.middleware.use PDFKit::Middleware
end
2) Have you installed wkhtmltopdf?
https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF
3) You need a multithreaded server, like unicorn:
in Gemfile:
gem 'unicorn'
in config/unicorn.conf:
worker_processes 3
to run server:
unicorn_rails -c config/unicorn.conf

Related

How to autoload classes to use in controller

I have a class in libs and try to use it in a controller. However I cannot access it. I tried to use the autoload function, but that doesn't work, and it shouldn't work in rails 5 in production mode, so I guess I don't need to try this one.. I also tried to require it in my controller, but I don't get the syntax correct I guess. I'm also wondering where to put my class, since I have read several different opinions..
config/application.rb
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Qipmatedevel
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
config.active_job.queue_adapter = :sidekiq
config.autoload_paths += %W(#{config.root}/lib)
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
end
app/controller/imports_controller
class ImportsController < ApplicationController
require 'lib/class_qip'
adding
require './lib/class_qip.rb'
fixed it
Put your classes in lib directory only & load as,
config.eager_load_paths << Rails.root.join('lib')
It works on both development and production environment.

Getting started with Nokogiri in Rails? No need for gem 'nokogiri'?

I'm just looking into Nokogiri and was thinking about using it in my app, but apparently when I do bundle install (without gem 'nokogiri') it's already "Using nokogiri 1.6.7.1".
When I add gem 'nokogiri' in my Gemfile, there's no "installing..." So, is nokogiri already pre installed in Rails? If so, do I still have to require these:
require 'nokogiri'
require 'open-uri'
Where do I put this? Within my controller? or application.rb?
This is my application.rb looks like
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
if Rails.env.test? || Rails.env.development?
CONFIG = YAML.load(File.read(File.expand_path('../aws.yml', __FILE__)))
CONFIG.merge! CONFIG.fetch(Rails.env, {})
CONFIG.symbolize_keys!
end
module App
class Application < Rails::Application
config.middleware.use Rack::Pjax
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq
end
end
Nokogiri is required by another gem (rails-dom-testing). So it's already installed.
And you don't need to write require 'nokogiri' statement. Because Rails uses Bundler to manage dependencies and load gems. Nokogiri will be already loaded.

Why is "require 'rails/all'" not installed/required in my config/application.rb?

This is just a curious irritant, but why does my app not include the expected line in config/application.rb, or anywhere else?
require 'rails/all'
This app was generated using Rails Composer in early 2014, if that makes a difference. Also, it is Rails 4.2.1.
The issue arose only because I am studying the Configuring Rails Applications and the The Rails Initialization Process guides as I have a need to modify my initialization process. Both state that the config/application.rb file is expected to contain that line, but mine does not. And, yes, the app runs just fine locally and on Heroku, so... Why?
My file is:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module Theappname
class Application < Rails::Application
config.generators do |g|
# Enable Chrome Source Maps so CSS and JS can be debugged
#g.sass_options = { :debug_info => true }
# don't generate RSpec tests for views and helpers
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
g.view_specs false
g.helper_specs false
end
# Rails 4 should include all helpers for controllers and views
config.action_controller.include_all_helpers = true
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
#config.time_zone = 'Eastern Time (US & Canada)'
config.time_zone = 'UTC' # Don't use local time or you won't notice time issues.
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not check for unavailable locales
I18n.enforce_available_locales = false
# not needed at 4.0
config.assets.initialize_on_precompile = false
# Load files in lib
config.autoload_paths += %W(#{config.root}/lib)
# Extend Rails classes in lib/core_ext/<classname>.rb... See above?
#config.autoload_paths += Dir[File.join(Rails.root, "lib", "core_ext", "*.rb")].each {|l| require l }
# 20150711 Default Date formats
#default_date_formats = { :default => '%d.%m.%Y' }
default_date_formats = { :default => '%Y.%m.%d' }
Time::DATE_FORMATS.merge!(default_date_formats)
Date::DATE_FORMATS.merge!(default_date_formats)
# 20150808 Adding Delayed_Job queueing for daily_report and such
config.active_job.queue_adapter = :delayed_job
end
end
You can require 'rails/all' if that suits your fancy, but the parts of rails necessary to run your application are required with these lines:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
If I were to guess the motivation for this, it is probably that you don't necessarily need or want all of the parts of rails in your application, so better to explicitly require the ones you want instead of everything. In this case if you were to require 'rails/all' you would end up with action_view, active_job, and rails/test_unit as well as the above. The files required can be found in railties.

How is this an un-initialized constant. (Mixins in rails)

under lib/ I have 'aisis_writer/loader.rb' which, inside that file, looks like:
module AisisWriter
module Loader
end
end
I then have, in my application.rb the following set up:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module AisisWriter
class Application < Rails::Application
# Load Lib
config.autoload_paths += %W(#{config.root}/lib)
# Use Rack Attack For Throttling
config.middleware.use Rack::Attack
end
end
From there I did, in the ApplicationController.rb: include AisisWriter::Loader and then I ran my tests and got:
'<class:ApplicationController>': uninitialized constant AisisWriter::Loader (NameError)
Either I cannot do what I am doing because of naming conflicts or I am doing something wrong. Any one care to tell me what I might be doing wrong?
I don't think your config.autoload_paths is broad enough -- it's not including subfiles of the lib directory.
This should do the trick:
config.autoload_paths += Dir[Rails.root.join('lib', '{**/}')]
Try defining like this within 'aisis_writer/loader.rb'
module AisisWriter::Loader
end

Rails engine extends config/application.rb

I am in the process of writing a Rails engine but i am not sure how to extend my the config/application.rb
I guess i have to get to the application name somehow
any ideas?
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module application_name
class Application < Rails::Application
end
end
For a --full and --mountable engine
This will be generated for you.
module engine_name
class Engine < ::Rails::Engine
end
end
In you main applications gemfile add
gem 'engine_name', path: "/path/to/engine_name"
And in your applications config/routes.rb file
mount engine_name::Engine, at: "/<mount_point_you_choose>"
http://guides.rubyonrails.org/engines.html
Taken from the link above...
The --mountable option tells the generator that you want to create a "mountable" and namespace-isolated engine. This generator will provide the same skeleton structure as would the --full option, and will add:
Asset manifest files (application.js and application.css)
A namespaced ApplicationController stub
A namespaced ApplicationHelper stub
A layout view template for the engine
Namespace isolation to config/routes.rb:

Resources