I am writing a Ruby on Rails 4.1 app with Ruby 2.11. The structure is Domain -> Team -> User. So, a user belongs to a team and I sometimes need to make a team from a domain (so they are associated). Think Starbucks -> NY Central Team -> Mr Barista.
I have made some builder classes and put them in app/builders, but when I try to use the class it says uninitialized constant in the Rails console. So, for example, I have the file in app/builders/team_builder.rb:
class TeamBuilder
attr_reader :domain, :params
def initialize(domain, params = {})
#domain = domain
#params = params
end
def build
domain.teams.build(params)
end
end
But when I type TeamBuilder.new(domain, name: 'Team name here!') I get the response
NameError: uninitialized constant TeamBuilder
It seems like it does not recognise the new class I have added above, which makes me think it is not loading it. But I thought that all sub-directories in app/ were loaded.
Totally stumped on this one, and I cannot find a guide or documentation on this (maybe it's there - somewhere...)
Go into your rails console (rails c) and type the following:
YourAppName::Application.config.eager_load_paths
(where YourAppName is whatever it is called in your environment.rb where initialize! is called). This should show you all the paths automatically loaded into your application (before customizing).
It looks like the automatic path adding magic is happening in here https://github.com/rails/rails/blob/master/railties/lib/rails/engine/configuration.rb
The docs indicate you can run:
bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'
to view in terminal
Related
Testing ruby-asterisk manager interface with ruby version 1.9.3p0 and gem 1.8.11, for all command and methods its printing the the same output.
Anyone faced similar problem.
Code:
#!/usr/bin/env ruby
require 'ruby-asterisk'
#ami = RubyAsterisk::AMI.new("192.168.1.5",5038)
#ami.login("admin","passs")
puts #ami.command("sip show peers")
Output:
#<RubyAsterisk::Response:0x000000016af710>
Project URL
Problem solved. Didn’t check the readme RESPONSE OBJECT section.
It's working.
var = #ami.command(""sip show peers)
puts var.data
You are putting the Instance of the RubyAsterix. I think after haveing a brief look at the project that most/all of the instance methods returns the instance it self. The reason for doing it that way is that it makes it very easy to chain multiplie actions which makes for a nice syntax/usage.
I think you should remove the puts and allow the gem to display what it wants to display.
In Octopress, I'm trying to get a post's full file path (something like ~/projects/site/source/_posts/2012-01-01-something.markdown) by extending the Jekyll:Post class.
module Jekyll
class Post
alias_method :original_to_liquid, :to_liquid
def to_liquid
# test if this function is actually called
puts "hello"
original_to_liquid.deep_merge({
'full_path' => File.join(#base,#name)
})
end
end
end
I name this file as full_path.rb and put it in the plugins folder. Oddly, my to_liquid function never get called, since the hello message didn't show up.
Even more strange, I find the date.rb shipped with Octopress also defines to_liquid method of class Post, so I add the full_path => File.join(#base,#name) line there and it works! I'm soooo confused.
So my question is, why my to_liquid method didn't get called?
UPDATE
After upgrading jekyll from 0.12.0 to 1.2.1, it magically works......
You might take a look at the Post#permalink documentation. It should do what you want without having to create new plugins.
(if I misunderstood you, maybe containing_dir is the method you're looking for)
I'm using "therubyracer" in a model, and I'm requiring at the top of the model as so:
require 'v8'
class Thing
def self.ctx; ##ctx ||= V8::Context.new; end;
def self.eval(script); ctx.eval(script); end;
end
However, I intermittently get:
NameError - uninitialized constant Thing::V8:
/app/thing.rb:3:in `ctx'
When testing requests through a local Padrino server, apparently after I modify code in Thing. This is corrected by restarting the padrino server. I'm assuming requiring v8 somewhere else would fix this problem, wheres the correct place?
This looks like it might be caused by the Padrino reloader getting confused when it reloads your thing.rb file, causing Ruby to look for V8 in the Thing namespace.
Try explicitly specifying V8 is in the top level using the :: prefix:
def self.ctx; ##ctx ||= ::V8::Context.new; end;
You can put it wherever you want if you add it on the Gemfile. Did you added it?
Thanks!
Kaminari's URL generation omits the page param if it is generating the link back to the first page. However, the application is designed to select a random page if the page parameter is omitted. Kaminari's default behaviour, then, precludes paginating back to the first page in a reliable way.
I've resolved this issue, and will post my solution below a bit later, but I wanted to post this question for posterity, and I'm also pretty new to Rails, thus I'm not sure my solution is the best or most elegant, and I'm interested in improvements and refinements, if just for my own selfish edification!
The line of code in Kaminari that implements the behaviour we want to change is in lib/kaminari/helpers/tags.rb, in the method Kaminari::Helpers::Tag::page_url_for.
def page_url_for(page)
#template.url_for #template.params.merge(#param_name => (page <= 1 ? nil : page))
end
To override this behaviour, I created a file lib/kaminari/helpers/tag.rb, containing the following:
module Kaminari
module Helpers
class Tag
def page_url_for(page)
#template.url_for #template.params.merge(#param_name => (page < 1 ? nil : page))
end
end
end
end
I then patched in the file by adding the following line to config/initializers/extensions.rb:
require "lib/kaminari/helpers/tag.rb"
My apologies for any awkwardness with the Ruby/Rails terminology, I'm still fairly new to Ruby. Comments and criticisms are welcome.
UPDATE
The new version of the kaminari source will require this as the updated line:
#template.url_for #params.merge(#param_name => (page))
Otherwise you will lose other params passed into your pagination call.
For clairity sake here is the full output of the new code:
module Kaminari
module Helpers
class Tag
def page_url_for(page)
#template.url_for #params.merge(#param_name => (page))
end
end
end
end
You will still place this inside an initializers file as Daniel suggested.
As of today (July 2016), the Kaminari master branch includes a config option params_on_first_page, which is false by default.
Setting this config option to true will include page params for all pages, including page 1.
Note that the master branch isn't a stable release, so use with caution!
This is the answer for 2018 as am writing this :
Like it's stated in the kaminari github home page
Run this to create a config file for kaminari :
rails g kaminari:config
This will create a file kaminari_config.rb in your config/initializers folder
Uncomment the line : config.params_on_first_page = false and replace false by true :
config.params_on_first_page = true
Restart your server if necessary. That's it :)
I've seen this asked a few times in other threads, but none of the answers seem to apply.
Environment:
Rails 3
amazon/ecs gem from jugend. The lone file is here:
http://github.com/jugend/amazon-ecs/blob/master/lib/amazon/ecs.rb
my gemfile has:
gem 'amazon-ecs', :git => 'git://github.com/jugend/amazon-ecs.git'
Everything works in irb. I can run:
bundle console
require 'amazon/ecs' and then go to town
when I try to use it from the controller though, like so:
require 'amazon/ecs'
require 'amazon/ecs'
class SearchController < ApplicationController
def index
end
def results
Amazon::Ecs.configure do |options|
options[:aWS_access_key_id] = '[key]'
options[:aWS_secret_key] = '[secret]'
end
res = Amazon::Ecs.item_search(params[:search], {:response_group => 'Medium', :search_index => 'All'})
end
end
I get: uninitialized constant SearchController::Amazon at line 8, where I first try to use Amazon.
the ecs.rb has a module Amazon containing a class Ecs. I'm not sure why this is working in erb, and not in rails.
I'm still kinda new to Rails, so please answer using small words. :-/
Was given the answer. I moved my initialization code to an initializer in config/initializers file, removed the require entirely, and things worked. I don't know why though, so if someone could answer that, that'd be great.
All of the gems require their files by default, so usually you don't need to explicitly require any files.
Speaking about your problem, it could somehow be, that your controller is run before Amazon module is processed.