In my adhearsion dialplan, I have the following code that is causing an immediate disconnect from the call without any output to the log or console:
the_flow = CallFlow.where(:dnis => dnis).first
CallFlow is a model in my rails app (gui/app/models/call_flow.rb), which lives in the gui directory of my adhearsion app. In my .ahnrc file I have:
paths:
# All paths are relative to this file's directory
init: config/startup.rb
dialplan: dialplan.rb
events: events.rb
models: gui/app/models/*.rb
And this is call_flow.rb:
class CallFlow < ActiveRecord::Base
belongs_to :routable, :polymorphic => true
def dialplan
puts self.routable.description.squeeze("\n").strip
end
def target_route=(params)
self.routable = params[:kind].constantize.new(params.reject {|k,v| k == "kind"})
end
end
And finally, I have the following line in config/startup.rb:
config.enable_rails :path => 'gui', :env => :development
I know the model works because I can create records using the rails server. But I don't even know how to get any information about what's going on to make the dialplan disconnect the call when it gets to that first line above.
Some things to check:
Ensure you have set logging to :debug in config/startup.rb
Ensure you have enabled either Rails integration or database integration, not both.
If you are running a version of Adhearsion prior to 1.1.0, some exceptions that occur in dialplan.rb may be silently lost. Consider upgrading to 1.1.0 or later (1.2.0 is current stable) and create an exception handler. This can be a simple message logger or you can report exceptions to Airbrake. See the bottom of this post for a simple Adhearsion exception logger.
Try starting the Adhearsion console to see if your models are loaded at all. Start the Adhearsion console with ahn start console /path/to/ahn/app. You will then have a console similar to the Rails console and should have access to all your ActiveRecord models (assuming the Rails integration loaded correctly).
Example exception logger for Adhearsion 1.1.0 or later. Put this in your events.rb:
events.exception.each do |e|
ahn_log.error e.message
ahn_log.debug e.backtrace.join("\n")
end
General notes on Rails vs. Database integration for Adhearsion:
For Rails integration have a line something like config.enable_rails :path => '/path/to/rails/app', :env => :production
For database integration, use something like:
config.enable_database :adapter => 'mysql',
:username => 'root',
:password => '',
:host => 'localhost'
For database integration only (not Rails integration), you should make sure that your models are in a place where Adhearsion can find them. The default location is models/ but this can be changed by editing the .ahnrc file in the Adhearsion app's base directory.
try to run this code in rails console
first start the console
bundle exec rails console
and then try to run the code which is causing the issue
CallFlow.where(:dnis => "something").first # replace "something" with something valid
Related
I have been learning Ruby on Rails, but I still have issues when it comes to Ruby gems with examples that are irb based and not Rails or Sinatra framework based. I am trying to implement the Block.io Bitcoin API functionality. But the code I find is Ruby only, so I am not sure where to create a config file for the API Key and also whether I need to create a controller to make this work in the views for Rails.
The gem and examples are on: https://github.com/BlockIo/gem-block-io
I installed this gem via bundle install on Rails
gem install block_io -v=1.0.6
The Ruby example show the following:
>> require 'block_io'
>> BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2
In Rails which config file would I enter the above api_key and pin?
In the example they show the code to get your address as follows:
BlockIo.get_my_address
Do I need to create a function in a controller such as:
def address
#my_address = BlockIo.get_my_addresses
end
and in the view use:
<%= #my_address %>
I need some guidance with regards to the above, any comment or assistance will be greatly appreciated.
require 'block_io' can go into Gemfile like gem 'block_io'. Rails/bundler will require it automaticaly for you as long as the gem name is also the file name you want to require from this gem.
BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2 can be put into an initilizer like config/initializers/block_io.rb. This way set_options is called only once when Rails starts a server or console or runner.
Put it like this into the file config/initializers/block_io.rb
BlockIo.set_options :api_key=> ENV['BLOCK_IO_API_KEY'], :pin => ENV['BLOCK_IO_PIN'], :version => 2
With the environment variables in use you don't commit any secret into your repo.
Now you should be able to call BlockIo.get_my_address within any action.
I'm seeing some weird behaviour in my Rails app. I'm running ruby 1.9.2-p290, and I have this sort of controller:
class NumbersController < ApplicationController
def index
render :json => [1,2,3]
end
end
Imagine I run the servers like this to demonstrate the problem:
$ rails s # This one runs in "development" on port 3000
$ RAILS_ENV=production rails s -p 2999 # This one runs in "production" on port 2999
In development or test mode, my result would be
$ curl localhost:3000/numbers # development
{numbers: [1,2,3]} # The root is being included in the json, as inferred from the controller name.
$ curl localhost:2999/numbers # production
[1,2,3] # No root in the JSON
I've been over the app with a fine tooth comb and there are no obvious configuration differences that look like they would affect json between development and production. Also, no lines like "if Rails.env === 'production'"
I'm guessing that different gems are being required, e.g. for assets, which are changing the behaviour of render :json => ... . I've inspected the versions of the "json" and "multi_json" gems from within the running apps, and they are the same (1.7.5 and 1.3.6 respectively, and multi_json uses the same adapter.). How do I find out exactly which gems are required from within the app while it is running? Also, does anyone have any alternative explanations?
EDIT: I'm running Rails 3.1.1 and the assets part of my Gemfile is:
group :assets do
gem "ember-rails"
gem "jquery-rails"
gem "less", "2.0.7"
gem "less-rails", "2.0.2"
gem 'uglifier'
end
I found a workaround here: include_root_in_json from controller
The solution:
render :json => {:numbers => [1,2,3]}, :root => false # If you want the root
:root => true isn't being respected in production. I suspect the as_json or to_json has been overridden incorrectly in that environment.
I'm still not happy though, because I can't rely on render :json => [1, 2, 3].
This behavior is controlled by this setting in config/initializers/wrap_parameters.rb for new rails apps.
# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
self.include_root_in_json = false
end
It could be that because your controller doesn't reference any AR (ActiveRecord) classes, AR isn't yet loaded due to lazy loading in development mode. Hence the setting is true.
You could test that theory by putting ActiveRecord::Base in your index action to force it to load.
I'd be looking for the following configuration:
ActiveRecord::Base.include_root_in_json
It produces exactly the difference you are getting between the JSON representations of object: if set to true the Object name is added as a root key of the produced JSON.
How do I find out exactly which gems are required from within the app
while it is running?
The Gemfile.lock file specifies this.
I'm trying to use oauth-plugin on a Rails application I'm developing, but I keep running into problems.
To make sure I'm not making any mistake, I started an application from scratch (using Rails 3.0.3). Here are the steps I followed:
Create da new rails application (rails.test)
Edited its Gemfile to include:
gem "oauth-plugin", ">=0.4.0.pre1"
gem "oauth", "0.4.4"
Generated oauth-consumer, by running script/rails g oauth_consumer
Edited oauth_consumers.rb to include my keys for Google integration:
:google=>{
:key=>"anonymous",
:secret=>"anonymous",
:scope=>"https://www.google.com/calendar/feeds/",
:options => {
:site => "http://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
},
}
Edited routes.rb to add the route for oauth_consumer:
resources :oauth_consumers
Edited application_controller.rb to implement the logged_in? method as follows:
def logged_in?
true
end
Now when I access http://localhost:3000/oauth_consumers/google I get the following error:
uninitialized constant GoogleToken
Does anyone know what causes this error and how can I fix it? GoogleToken is a class that should have been auto generated by oauth-plugin, so I can't tell why I'm getting this uninitialized constant error.
The GoogleToken class doesn't get auto-generated unless you pass "google" to the generator like so:
script/rails g oauth_consumer google
or for rails 3:
rails g oauth_consumer google
Also check to ensure the relationship is set up in the user model like so:
has_one :google, :class_name => "GoogleToken", :dependent => :destroy
Did you remember to run bundle install from terminal after editing your Gemfile? Sounds like your Rails app doesn't know about these gems yet.
I have the same problem, i think a solution could be in this:
https://github.com/pelle/oauth-plugin/blob/master/lib/generators/oauth_consumer/USAGE
You need some sort of authentication like restful-authentication plugin, if you uncomment line 27..29 in your oauth_consumers_controller.rb file, you'll jump to next step!
I'm trying to get a sinatra app as a subpath in my rails 3 app.
Specifically, the resque queuing system has a sinatra based web interface that I would like to have accessible through /resque on my usual rails app.
You can see the project here: http://github.com/defunkt/resque
I found some people talking about adding a rackup file and doing this sort of thing:
run Rack::URLMap.new( \
"/" => ActionController::Dispatcher.new,
"/resque" => Resque::Server.new
)
But I don't really know where to put that or how to make it run. My deployment is with passenger, but it would me nice to also have it running when I run 'rails server' too. Any suggestions?
--edit--
I've made some progress by putting the following in config/routes.rb:
match '/resque(/:page)', :to => Rack::URLMap.new("/resque" => Resque::Server.new)
Which seems to work pretty well, however it loses the public folder, (which is defined within the gem I guess), and as a result, there is no styling information, nor images.
You can setup any rack endpoint as a route in rails 3. This guide by wycats goes over what you are looking for and many of the other things you can do in rails3:
http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/
For example:
class HomeApp < Sinatra::Base
get "/" do
"Hello World!"
end
end
Basecamp::Application.routes do
match "/home", :to => HomeApp
end
Yehuda (/Scott S)'s solution doesn't work for me with Rails 3.0.4 and Sinatra 1.2.1... setting :anchor => false in the matcher is the key:
# in routes.rb
match "/blog" => MySinatraBlogApp, :anchor => false
# Sinatra app
class MySinatraBlogApp < Sinatra::Base
# this now will match /blog/archives
get "/archives" do
"my old posts"
end
end
(answer c/o Michael Raidel - http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/)
I wrote a little monkeypatch to the Rails MySQLAdapter and want to package it up to use it in my other projects. I am trying to write some tests for it but I am still new to testing and I am not sure how to test this. Can someone help get me started?
Here is the code I want to test:
unless RAILS_ENV == 'production'
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter < AbstractAdapter
def select_with_explain(sql, name = nil)
explanation = execute_with_disable_logging('EXPLAIN ' + sql)
e = explanation.all_hashes.first
exp = e.collect{|k,v| " | #{k}: #{v} "}.join
log(exp, 'Explain')
select_without_explain(sql, name)
end
def execute_with_disable_logging(sql, name = nil) #:nodoc:
#Run a query without logging
#connection.query(sql)
rescue ActiveRecord::StatementInvalid => exception
if exception.message.split(":").first =~ /Packets out of order/
raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
else
raise
end
end
alias_method_chain :select, :explain
end
end
end
end
Thanks.
General testing
You could start reading about testing.
After you are understanding the basics of testing, you should think what you have changed. Then make some tests which test for
the original situation, resulting in errors since you updated it. So reverse the test after it indeed is working for the original situation.
the new situation to see whether you have implemented your idea correctly
The hardest part is to be sure that you covered all situations. Finally, if both parts pass then you could say that your code it working as expected.
Testing gems
In order to test gems you can run
rake test:plugins
to test all plugins of your rails application (see more in chapter 6 of the testing guide), this only works when the gem is in the vendor directory of an application.
Another possibility is to modify the Rakefile of the gem by including a testing task. For example this
desc 'Test my custom made gem.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
would run all available tests in the test directory ending with _test.rb. To execute this test you can type rake test (from the gem directory!).
In order to run the tests for the gem by default (when typing just rake) you can add/modify this line:
task :default => :test
I used the second method in my ruby-bbcode gem, so you could take a look at it to see the complete example.