I recently developed a simple RubyGem and deployed it to RubyGems.org. How do I use this RubyGem within a Rails application? Here is the code to the Gem
module Gem1
class Message
def self.gem?
puts "Hi This is a RubyGem"
end
end
end
You can specify the gem in your Gemfile and do a bundle install.
Typically can be done
gem 'nokogiri'
in your Gemfile and run bundle install.
once the gem is installed you can access it across your application
Related
I am making a rails plugin named signup.
In this plugin, I used bcrypt gem, Here is my engine.rb
module Signup
class Engine < ::Rails::Engine
isolate_namespace Signup
require 'rubygems'
require 'bcrypt'
end
end
My Gemfile:
source 'https://rubygems.org'
gemspec
gem 'bcrypt'
I follow this Rails Engine - Gems dependencies, how to load them into the application?
But In my demo application, when start rails server, then I got this error:
lib/signup/engine.rb:9:in `require': cannot load such file -- bcrypt (LoadError)
I want to load bcrypt gem in my demo application. If I add gem 'bcrypt' in my demo application's Gemfile and run bundle install, it does not show any kind of error, But I don't want to do it.
I want that gem 'bcrypt' will be load automatically in my demo application without adding this in Gemfile.
I am using pry gem for debugging my rails application. So I have
specified it in my Gemfile and using it. But I dont need to push the
Gemfile with pry gem specified. So everytime I need to revert it to
original Gemfile and push it to remote repo.
I was wondering is there any way where I can install pry gem in my
local machine and use it in my rails app globally so that I dont have
to specify that gem in Gemfile.??
I tried to do
gem install pry
But when I use binding.pry in my rails controller, it says
undefined method `pry' for #<Binding:0x0000000619398>
With bundler you can create groups according to your environments.
You can intall your gem only for the development group.
Put the line below in your gemfile.
gem 'pry', :group => :development
My Rails 4.2 app fails to load some GitHub hosted gem (installed via bundler v1.8.2). I am using rvm 1.26.10 (master)
In my Gemfile,
gem 'simple_form', '~> 3.1.0'
gem 'actionform', :github => 'rails/actionform'
Bundler install them in different location:
$ bundle show simple_form
/Users/me/.rvm/gems/ruby-2.1.5#my_app/gems/simple_form-3.1.0
$ bundle show actionform
/Users/me/.rvm/gems/ruby-2.1.5#my_app/bundler/gems/actionform-4a858fecf4c2
Rails never load the actionform gem.
After inserting the line //= require action_form to my app/assets/javascript/application.js file, this error comes
Sprockets::FileNotFound at / couldn't find file 'action_form'
However, the action_form.js file exists in the gem file.
Moreover, when i try to reproduce the readme example, i got this error
NameError at /conferences/new uninitialized constant ActionForm
require 'bundler/setup' is in boot.rb
Any advise about this issue?
Thanks!
The problem is a mismatch between the gem name and the file inside the gem. Because the gem is named 'actionform', Bundler will try to require 'actionform', however the file is actually called action_form.
You can tell Bundler the right file name with this syntax:
gem 'actionform', :github => 'rails/actionform', :require => 'action_form'
Note that it is normal for gems from git sources to be installed into a different location than gems installed from gem servers. It has nothing to do with this problem.
I have just configured dependencies for a simple Rails application using Gemfile, but I'm not able to start it anymore.
Here is the error message I get:
/Library/Ruby/Site/1.8/rubygems.rb:274:in `activate': can't activate rails (= 2.3.5, runtime) for [], already activated rails-3.0.4 for [] (Gem::LoadError)
from /Library/Ruby/Site/1.8/rubygems.rb:216:in `try_activate'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
...
The Gemfile is configured as follows
source :rubygems
gem 'rails', '3.0.4'
#gem 'rails', '2.3.5'
gem 'fastercsv', '1.5.4'
gem 'comma', '0.4.1'
and my environment.rb contains the following line
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '3.0.4' unless defined? RAILS_GEM_VERSION
Note that if I change that variable to 2.3.5, the server starts.
Any idea about what is causing the issue?
If you want to change an existing gemfile run: bundle update. Bundle install is only used for the initial setup. And by the way we are at 3.0.5 now. I am also wondering was this a rails 2 application? Did you simply swap out the gem version number and expect it to then become a rails 3 app? The environment.rb file in a rails 3 application does not show the rails gem version.
I wrote a plugin that requires a gem as a dependency.
Where do I have to define this dependency?
I have tried to create a Gemfile in vendor/plugins/my_plugin/, but bundle install doesn‛t find this file.
Ok. I have solved.
1) Create a Gemfile in vendor/plugins/my_plugin like:
# Gemfile
source "http://rubygems.org"
gemspec
2) Create a gemspec file. In the folder vendor/plugins run this command:
bundle gem my_plugin
(Note this command ask you for overwrite some files. Check the files before answer: Y)
3) Open gemspec file in vendor/plugins/my_plugin/ and add before the keyword end:
s.add_dependency('will_paginate', '~> 3.0.pre2')
(In this example I have used will_paginate how required dipendency of my_plugin)
4) Now go in your rails app and edit Gemfile, add:
gem 'my_plugin', :path=>'vendor/plugins/my_plugin'
The path specified supposed that your plugin is already in vendor/plugins folder of your rails app.
Of course when deploy rails app you don't need anymore to specify :path argument.
5) Now in rails app root do:
bundle install
And dependency of my_plugin (will_paginate in this case) is installed.
Thank to Sinetris for initial input.
Create a Gemfile in your vendor/plugins/my_plugin/ like:
# Gemfile
source "http://rubygems.org"
gemspec
gem "your-dependency-gem-name"
note the gemspec directive.
Take a look at Using Bundler with Rubygem gemspecs for more information.
Sebtm's own answer is quite good, but it still didn't work as Tiago and orangechicken described. I had to add
require 'your-dependency-gem-name'
on top of lib/my_plugin.rb right before the engine of my_plugin is loaded.
See http://guides.rubyonrails.org/engines.html#other-gem-dependencies
Gemfile in the application folder.
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3
Reference for myself. If you're making a plugin that should work with Rails as a RailTie, use rails plugin my_plugin to create the gem instead of bundle gem my_plugin. It saves you (me) a lot of trouble.
Edit: When do you need the gem to work as a RailTie? Whenever you want to add rails generator, rake tasks or add your code to a Rails app (Called Rails Engine).
/Edit
$ rails plugin new my_plugin
then you add dependencies
Gem::Specification.new do |s|
#...
s.add_dependency "rails"
s.add_runtime_dependency "heroku"
s.add_development_dependency "sqlite3"
end
to include it in your rails app, use path: as described by #Sebtm
or release it to rubygems.
$ gem build my_plugin.gemspec
$ gem push my_plugin-0.7.0.gem #replace version number
#in Gemfile, as always
gem 'my_plugin'
or tag it on github. (use their release UI) and depend on it using
gem 'my_plugin', github: 'accountname/my_plugin', tag: 'v0.7.0'