Rack Error -- LoadError: cannot load such file - ruby-on-rails

Trying to go through the tekpub rack tutorial but run into this error.
Boot Error
Something went wrong while loading app.ru
LoadError: cannot load such file -- haiku
There is a file named haiku.rb in the same directory as the app I am trying to run but I get the above error while trying to run the program. Here is the code:
class EnvironmentOutput
def initialize(app=nil)
#app = app
end
def call(env)
out = ""
unless(#app.nil?)
response = #app.call(env)[2]
out+=response
end
env.keys.each {|key| out+="<li>#{key}=#{env[key]}</li>"}
["200",{"Content-Type" => "text/html"},[out]]
end
end
require 'haml'
require 'haiku'
class MyApp
def call(env)
poem = Haiku.new.random
template = File.open("views/index.haml").read
engine = Haml::Engine.new(template)
out = engine.render(Object.new, :poem => poem)
["200",{"Content-Type" => "text/html"}, out]
end
end
use EnvironmentOutput
run MyApp.new
I'm sure its a small error as the code is the same as in the tutorial and it works for him...
Thanks

You'll want to read up on ruby load path (either $LOAD_PATH or $:). By default, ruby has a load path which includes wherever your gems are installed, which is why you can do require 'haml' without providing the full path to where your haml gem is located.
When you type require 'haiku', you're basically telling ruby to look for some file called haiku.rb somewhere in it's load path, and the LoadError comes from ruby not finding your haiku.rb file in any of the directories listed in $LOAD_PATH (or $:, which is just shorthand for $LOAD_PATH).
You can solve this in one of (at least) two ways:
change require 'haiku' to require File.dirname(__FILE__) + '/haiku.rb' to explicitly tell ruby what file to load
add the current working directory to your load path: $:.push(File.dirname(__FILE__)). This way you can keep the require 'haiku' part.

Related

LoadError (Unable to autoload constant XYZ, expected XYZ.rb to define it) in development environment

Recently, I can't make changes to my app without restarting my development server, otherwise I receive this error:
LoadError (Unable to autoload constant BotFeedback, expected ../../bot_feedback.rb to define it)
This hasn't been a problem before and I'm not entirely sure why this has become a problem. I have these settings configured in application.rb:
# Auto-load the bot and its subdirectories
config.paths.add File.join('app', 'bot'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'bot', '*')]
My app/bot folder includes files such as:
bot.rb with:
require "#{Rails.root}/app/bot/orderbot.rb"
Bot.on :message do |message|
OrderBot.new()
..
end
def somefunction
OrderBot.new()
..
end
orderbot.rb with:
require "#{Rails.root}/app/bot/bot_feedback.rb"
require "#{Rails.root}/app/bot/bot_demo.rb"
require "#{Rails.root}/app/bot/bot_helper.rb"
class OrderBot
include BotFeedback
include BotDemo
include BotHelper
(many more includes)
..
end
bot_feedback.rb with:
require "#{Rails.root}/app/models/concerns/sharedmethods.rb"
class OrderBot
include Sharedmethods
module BotFeedback
...
end
end
bot_demo.rb with:
class OrderBot
module BotDemo
..
end
end
bot_helper.rb with:
require "#{Rails.root}/app/models/concerns/sharedmethods.rb"
class OrderBot
include Sharedmethods
module BotHelper
...
end
end
My guess is that including the sharedmethods file is causing this because I don't see anything else being a problem. Changing the sharedmethods file in the rails app has always seemed to require restarting the server.
I would appreciate any help/suggestions.
UPDATE:
Looks like using 'load' instead of 'require' seems to solve the problem. I'm not sure this is the right way to go about it though.. because it will take up more memory if the files are being loaded again and again?
load "#{Rails.root}/app/bot/bot_feedback.rb"
load "#{Rails.root}/app/bot/bot_demo.rb"
load "#{Rails.root}/app/bot/bot_helper.rb"
Finally figured out the answer after learning everything about rails 'autoload' and 'require'
I don't actually need to 'require' any of the files because they are already autoloaded. The culprit was the 'module', it wasn't necessary.

Sinatra and Rails - starting a submoduled app

I've been basing an app with a blogging app (octopress) submoduled, on the following guide:
http://www.nickhammond.com/setting-octopress-jekyll-blog-rails-application/
It's a bit out of date but other than having to change around some gems seems to be working fine. The guide suggests having a folder called 'Blog' in your main parent app directory, in which the smaller app lives. Here I was told to change the config.ru file, and rename it to run.rb. The file is as follows:
/blog/run.rb
require 'bundler/setup'
require 'sinatra/base'
# The project root directory
$root = ::File.dirname(__FILE__)
class Blog < Sinatra::Base
get(/.+/) do
send_sinatra_file(request.path) {404}
end
not_found do
send_file(File.join(File.dirname(__FILE__), 'public', '404.html'), {:status => 404})
end
def send_sinatra_file(path, &missing_file_block)
file_path = File.join(File.dirname(__FILE__), 'public', path)
file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i
File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
end
end
My config.ru file in my parent app is as follows:
/config.ru
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
require './blog/run'
# Action Cable requires that all classes are loaded in advance
Rails.application.eager_load!
map '/' do
run Rails.application
end
map '/blog' do
run Blog
end
When I visit localhost:3000/blog, I don't get a route not found error, however receive:
This localhost page can’t be found
No web page was found for the web address: http://localhost:3000/blog
Any help on how to properly set up my config.ru file would be greatly appreciated, thanks in advance.
Please try to move
require '/blog/run'
map '/blog' do
run Blog
end
to the routes.rb and see if this helps.
Update: (at the end it said to change your config.ru to this).
require ::File.expand_path('../config/environment', __FILE__)
require './blog/run' # Require the run.rb file that we created earlier which has the Rack setup for the blog
map '/' do # By default we want everything to hit our Rails application
run Rails::Application
end
map '/blog' do # Anything at blog/ and beyond will then hit the blog
run Blog
end
Please give this a try.

No such file to load -- 'gem name' using require

I am using gem differ https://github.com/pvande/differ
I have a helper
require 'differ'
module AnswersHelper
def self.getDiff (text1, text2)
Differ.format = :html
diff = Differ.diff_by_word(#current, #original)
end
end
But I get an error No such file to load -- differ
If I remove require line
I get an error at that line
Differ.format = :html
uninitialized constant QuestionsController::Differ
When I tried following commands in rails console it worked
require 'differ'
diff = Differ.diff_by_word("text1","text2)
I have gem differ in my gemfile
and also I tried
require_relative 'differ'
and
require './differ'
UPD: seems restarting server helps, I'll check it right now
Restarting server helped........

How to use a RubyGems class in a Rails app

I need to stream a TAR file using data from the database in a Rails app.
I know that RubyGems has the TarWriter module that suits my use case perfectly.
The question is, how can I include the module in my Rails app?
I tried to require rubygems/package as follows:
require 'zlib'
require 'rubygems/package'
tar = StringIO.new
Gem::Package::TarWriter.new(tar) do |writer|
writer.add_file("a_file.txt", 0644) do |f|
(1..1000).each do |i|
f.write("some text\n")
end
end
writer.add_file("another_file.txt", 0644) do |f|
f.write("some more text\n")
end
end
tar.seek(0)
gz = Zlib::GzipWriter.new(File.new('this_is_a_tar_gz.tar.gz', 'wb'))
gz.write(tar.read)
tar.close
gz.close
The require failed on the controller. I tried to adding gem 'rubygems/package' on my Gemfile but it didn't work.
It's a noob question, how can I use RubyGems modules from a Rails app?
You should specify full path to module: Gem::Package::TarWriter and add require 'rubygems/package' to your controller.

Ruby on Rails docsplit file path

I have a pdftotext.rb file in /lib and the code is
module Pdftotext
require 'rubygems'
require 'docsplit'
class << self
def convert
Docsplit.extract_text("hello.pdf")
end
end
end
I have the hello.pdf file in the /assets folder and I tried "assets/hello.pdf" but it keeps telling me Error: Couldn't open file '/assets/hello.pdf': No such file or directory.
How can I get the right path to get the file to be converted?
By the way I am using rails 3.2.1, thanks.
Do you mean it is in RAILS_ROOT/assets/hello.pdf?
You should use File.join to get at the file. Like this:
module Pdftotext
require 'rubygems'
require 'docsplit'
class << self
def convert
Docsplit.extract_text(File.join(Rails.root, "assets", "hello.pdf"))
end
end
end
Using "/assets/hello.pdf" will try to get it from the file system root.

Resources