Getting started with Nokogiri in Rails? No need for gem 'nokogiri'? - ruby-on-rails

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.

Related

PDFKit Middleware doesn't work

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

If I require a file in application.rb does this make it available throughout the app?

Application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'parse-ruby-client'
Parse.init :application_id => "<APP_ID>",
:api_key => "<API_KEY>"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module GarmentsROR
class Application < Rails::Application
end
end
My gem parse-ruby-client doesn't seem to allow my rails app to connect to my parse.com account. I have a feeling I've put Parse.init in the wrong place.
The instructions weren't very clear. It assumes I should know where they go.
Would appreciate some help
You should create an initializer in config/initializers/parse_ruby_client.rb with:
Parse.init :application_id => "<APP_ID>",
:api_key => "<API_KEY>"
My issue was forgetting to remove the symbols < and > out of the APP ID and API KEY. Doing this solved all my issues.

Gem not working in rails project but in ruby file

When I run test.rb (see below) as a separate ruby file from within my rails project it works fine but when I wrap it as a module to be called from a controller it gives me:
LoadError (no such file to load -- eventmachine):1 in 'ModuleTest'
The gem is installed (sudo gem install event machine and bundle install) and added to the gem file (gem 'eventmachine').
Could someone please advice on what it is that I'm missing?
Separate file (called through: $ ruby lib/test.rb):
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'fiber'
def doStuff
end
doStuff
Module:
require 'eventmachine'
require 'em-http'
require 'fiber'
module ModuleTest
def doStuff
end
end
Controller:
require 'moduletest'
class MyController < ApplicationController
doStuff
end
Add
gem 'eventmachine'
Into your Gemfile. Then execute bundle install.
If you want to use a new gem in your rails project, you need to add it into Gemfile. To learn more, visit http://gembundler.com/

Including gems in a rake file

I'm trying to write a rakefile outside of a rails project.
I've created a new directory, added a rakefile and set a basic default task. It works.
I want to use the premailer gem so in my default task I've added this -
premailer = Premailer.new('http://localhost/email.html', :warn_level => Premailer::Warnings::SAFE)
This doesn't work, I get the following error.
uninitialized constant Object::Premailer
How do I refer to the premailer gem in my task? Should I be including it in a gemfile of sorts?
You can work without a Gemfile like this:
require 'rubygems' # only needed for ruby 1.8.7
require 'premailer'
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
or with a Gemfile:
require 'rubygems' # only needed for ruby 1.8.7
require 'bundler'
Bundler.setup
Bundler.require
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
I hope this helps.

How can you load the Rails environment from CloudCrowd actions?

I'm writing an "action" for CloudCrowd which needs access to the Rails environment (for some ActiveRecord stuff) but the standard means of loading the environment is resulting in fishy errors.
I tried each of the following at the top of my action .rb file:
require(File.join(File.dirname(__FILE__), '../..', 'boot'))
and
require File.expand_path(File.dirname(__FILE__) + "/../../environment")
When I try to start the node I get this error:
»crowd node
Starting CloudCrowd Node on port 9063...
Missing the Rails 2.3.2 gem. Please `gem install -v=2.3.2 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.
And I of course do have the gem installed:
»gem list | grep -i rails
rails (2.3.4, 2.3.2, 2.2.2, 1.2.6)
Nice!
I've actually had some trouble with your RAILS_ROOT path and replaced '../../..' with '../..'. Also, since you've already declared the RAILS_ROOT constant, you could chop off a few things in the environment requirement. Here's my version:
RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../..'))
RAILS_ENV = ENV['RAILS_ENV'] = ENV['RACK_ENV']
if CloudCrowd.node?
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "#{RAILS_ROOT}/config/environment"
# and if you need to import
# anything from lib just go ahead and
require 'my_custom_lib/name_of_file'
end
Somebody from #documentcloud saw my plea and helped me work through it. Had to prefix the action script with this:
RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../../..'))
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
if CloudCrowd.node?
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
require File.expand_path(File.join(File.dirname(__FILE__), '../..', 'environment'))
end

Resources