Rails own gem and uninitialized constant - ruby-on-rails

I am trying to made my own gem, but I am still getting an error,
Here is my gem structure:
order
- app
- controllers
- order_controller.rb
- lib
- order
order.rb
And content of the order_controller.rb:
class Order::OrderController < ApplicationController
def index
puts "asd"
end
end
I've got order gem added to the main application Gemfil.
I defined my root to lead to index action of that controller:
root :to => "order/order#index"
but when I try to access homepage I get:
uninitialized constant Order::OrderController
What I've done wrong?

Did you run bundle install after adding the gem in the Gemfile? By the way, why are you making a gem for that structure instead of making a Rails project with that structure?

Related

Whois gem not working in rails

class DomaincheckerController < ApplicationController
def index
end
def store
r =Whois.whois(secure_params['domain'])
render :text => "#{r}"
end
private
def secure_params
params.require(:whois).permit(:domain)
end
end
This is my domainchecker controller. The index method renders a form. After submitting the form it goes to store method. Here I am trying to use the whois gem. I have installed whois gem by running gem install whois. But I am getting this error.
uninitialized constant DomaincheckerController::Whois
The problem is that you installed the gem directly and not using bundler, therefore the Rails app can't find the dependency.
In order to install a gem in a Rails project you need to edit the Gemfile file and add the gem there. Once added, run
$ bundle
in order to install the dependency. Check the documentation about the Gemfile.

NoMethodError Ruby setup

I'm new to rails and i've tried to set up a new project. I'm running Ubuntu 14.
These are my steps:
rails new my_project -T this created my new project.
rake db:create this created my database
Then i edited my Gemfile and deleted gem 'sqlite3'
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'sqlite3'
end
bundle install --without production
rails g controller welcome index about to create controllers
As last step i edited config.rb:
root to: 'welcome#index'
Now, when i try to go to localhost:3000/welcome/index i get the following error:
Why? It should display /welcome/index with the placeholder HTML.
This is my Welcome Controller:
class WelcomeController < ApplicationController
def index
end
def about
end
end
In application.css -
Remove :
*= require_tree .
and try. I am sure this problem appears because of css.

Create a routes entry from your own gem

I'm writing a gem and I'm going to use it with Rails 4. Is it possible for me to add a route from my Gem rather than from config/routes.rb in my rails project? I want this to be inside a gem so I can include it in more than one Rails project without having to configure every Rails project, rather do it once in the gem. Is that possible and how?
i.e :
If my routes were :
get 'test' => 'users#test'
how would that translate into my gem. If my gem were used as an engine just like RB suggested in his answer :
module Blorgh
class Engine < ::Rails::Engine
get 'test' => 'users#test'
end
end
This doesn't work, what am I doing wrong?
Read the Engine Guide of Ruby on Rails. Basically you'll want to create the file in config/routes.rb (on your gem folder) and add the following:
YourGemName::Engine.routes.draw do
get 'test' => 'users#test'
end
Yes it is possible if you make your gem an engine.
Read the Getting Started with Engines guide.

Cannot find file or constant when using a gem locally in a Rails app

I have a gem that I am making and it is located at the same level as a rails app.
I am trying to use the gem in the rails app, but am not able to make it work - the constant I am trying to use, which is defined in the gem, is not accessible to the rails app for some reason.
What am I doing wrong here? What steps can I take to begin debugging the cause of the problem?
In the rails console, $:.grep /mygem/ shows me ["/Users/zabba/mygem/lib"]
Directory structure, with the contents of certain files:
~/mygem/
lib/
mygem/
some_class.rb
module Mygem
class SomeClass
end
end
mygem.rb
require 'mygem/some_class'
~/railsapp/
Gemfile:
gem 'mygem', path: '../mygem', require: 'mygem'
app/
models/
a_model.rb:
# require 'mygem' cannot find file
class AModel
def hello_world
SomeClass.new # Cannot find constant
Mygem::SomeClass.new # Cannot find constant
end
end
Can you create a folder vendor/gems and copy your gem in there. Then in you Gemfile
Gemfile
gem 'mygem', :path => "vendor/gems/mygem-0.0.1"
Then bundle install

no such file to load -- RMagick.rb Rails, Ubuntu

I have ruby and my gems installed in my home directory using rvm. 'require RMagick' works from the console and irb, but not from within my Rails app. I have the gem listed in my Gemfile.
require 'RMagick'
class PetsController < ApplicationController
def image
image = Image.new(image_path("base/Base.png"))
#image = Pet.composite(0, '4a4a4a')
#response.headers["Content-type"] = image.mime_type
render :text => image.to_blob
end
end
If I remove the require from my controller, I get
uninitialized constant PetsController::Image
I also have tried require 'RMagick' in boot.rb, and by referring to the Image object using Magick::Image (which gives me uninitialized constant PetsController::Magick.
What am I doing wrong?
Solved it! Restarting the server fixed it. I guess I hadn't restarted the server when I added rmagick to my gemfile.
Is it possible that you are running the rails app with another user than the one that has the RMagick gem installed?

Resources