gem install rubyoverflow
irb
> require 'rubyoverflow'
=> true
But:
require 'rubyoverflow'
include Rubyoverflow
class QuestionsController < ApplicationController
def question_by_tag
ruby_q = Questions.retrieve_by_tag('ruby')
Get error:
LoadError in
QuestionsController#question_by_tag no
such file to load -- rubyoverflow
Rails.root:
D:/artefacts/dev/projects/stack
app/controllers/questions_controller.rb:1:in
`'
This error occurred while loading the
following files: rubyoverflow
Is there any special rules to import moduled in the controller?
why do you use both require and include? include Rubyoverflow will be enough
UPD
For gem you should add it into your Gemfile (Rails 3.x) or config/environment.rb (Rails 2.x)
# Gemfile
gem "rubyoverflow"
# environment.rb
config.gem "rubyoverflow"
Then run bundle for Rails 3.x and rake gems:install for Rails 2.x
Related
It's possible, for example, to use ActiveRecord inline in a Ruby script. I like this a lot to report bugs, test features and share gists.
I'm wondering if the same could be done for a Rails webservice? (I'm mainly interested in getting the controller layer to work, the rest should be easy to add on demand.) Something along these lines:
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require 'rails/commands'
APP_PATH = File.expand_path('config/application', __dir__)
Rails::Command.invoke('server')
When toying around with this it did seem that an external entry point (APP_PATH) is required. So alternatively, an acceptable approach would be to cram all config into the single entry point. I couldn't get this to work so far.
The Rails Initialization Process is the best resource I found about this so far.
I produced a minimal Rails app to get started as follows:
rails new min-rails --skip-keeps --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-record --skip-active-storage --skip-puma --skip-action-cable --skip-sprockets --skip-spring --skip-listen --skip-javascript --skip-turbolinks --skip-test --skip-system-test --skip-bootsnap --api
cd min-rails
rm -rf app/jobs app/models config/initializers config/locales lib log public tmp vendor config/environments/test.rb config/environments/production.rb config/credentials.yml.enc config/master.key bin/rake bin/setup bin/bundle
I ended up with the following script:
inline-rails.rb
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require "action_controller/railtie"
class App < Rails::Application
routes.append do
get "/hello/world" => "hello#world"
end
config.consider_all_requests_local = true # display errors
end
class HelloController < ActionController::API
def world
render json: {hello: :world}
end
end
App.initialize!
Rack::Server.new(app: App, Port: 3000).start
Run it as:
ruby inline-rails.rb
We are upgrading Rails 3.2 engine to Rails 4.2.0 on ruby 2.0.0. Gem 'activerecord-session_store' was added to engine's gemspec following gem's instruction:
s.add_dependency 'activerecord-session_store'
and added following in initializers/session_store.rb under dummy:
Dummy::Application.config.session_store :active_record_store, :key => '_my_app_session'
then, we did bundle install. When we ran:
bundle exec rails generate active_record:session_migration
There is the error from the gem's generator:
/activerecord-session_store-0.1.1/lib/generators/active_record/session_migration_generator.rb:16:in `session_table_name': uninitialized co
nstant ActiveRecord::SessionStore (NameError).
We move the gem into engine's Gemfile and same error. Why the SessionStore is still not initialized?
Update
In engine's engine.rb under lib, the session table is pointed to:
initializer "Authentify.add_middleware" do |app|
ActiveRecord::SessionStore::Session.table_name = 'authentify_sessions'
app.middleware.use ActiveRecord::SessionStore
end
The setup works for Rails 3.2.
If you were not using the default table name for sessions, set:
ActiveRecord::SessionStore::Session.table_name = 'your_old_session_table'
in config/application.rb.
Additional configuration.
What we did is to add gem 'activerecord-session_store' to engine's Gemfile in addition to the .gemspec. The error disappeared.
I know how to create a rails generator gem which is called like:
rails g my_generator command
And I know how to create a thor gem which can be called like:
my_generator command
But I don't know how to create a rails generator that can be called using an executable. I have tried by creating a lib/my_generator/cli.rb file like:
require 'thor'
module Mang
class Cli < Thor
include ::Rails::Generators::Base
desc "install_gem", "install a gem"
def install_gem
gem 'thor', "0.18.1"
end
end
end
But I get the following error despite having added Rails as a dependency in my gemspec.
uninitialized constant Rails (NameError)
The fix was just a matter of including the rails/generators/actions module.
require 'thor'
require 'rails/generators'
require 'rails/generators/actions'
module Mang
class Cli < Thor
include Thor::Actions
include Rails::Generators::Actions
desc "install_gem", "install a gem"
def install_gem
gem 'thor', "0.18.1"
end
end
end
To include httparty in my rails 4.0.0 app, in my gemfile I wrote:
gem 'httparty'
and then ran bundle install
Next in my application.rb file, I inserted this:
module myApp
class Application < Rails::Application
### ---
config.gem "httparty"
### ---
end
Now when I load rails c and do a require "httparty", I get false
What am I doing wrong? How do I load httparty in my rails app?
It's already loaded by Rails. You also don't need the config.gem in application.rb
Try using HTTParty from the console. It should just work.
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/