Ive started working on an application called Shapado (http://shapado.com) which is running on Ruby with a mongo database in the background. Im getting the following error when I try to add some data to the database. I have attached all the relevant files that I think could be the source.
The error
Processing QuestionsController#tags_for_autocomplete to js (for 93.97.246.67 at 2011-03-31 10:02:00) [GET]
Parameters: {"tag"=>"backup"}
NoMethodError (undefined method `find_tags' for #<Array:0x7f24f1032448>):
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/proxy.rb:109:in `send'
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/proxy.rb:109:in `method_missing'
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/dynamic_querying.rb:37:in `method_missing'
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb:76:in `method_missing'
app/controllers/questions_controller.rb:184:in `tags_for_autocomplete'
app/controllers/questions_controller.rb:180:in `tags_for_autocomplete'
compass (0.10.5) lib/compass/app_integration/rails/actionpack2/action_controller.rb:7:in `process'
warden (1.0.3) lib/warden/manager.rb:35:in `call'
warden (1.0.3) lib/warden/manager.rb:35:in `call'
warden (1.0.3) lib/warden/manager.rb:34:in `catch'
warden (1.0.3) lib/warden/manager.rb:34:in `call'
haml (3.0.25) lib/sass/plugin/rack.rb:41:in `call'
/var/lib/gems/1.8/gems/mongomapper_ext-0.5.0/lib/mongomapper_ext/file_server.rb:17:in `call'
app/middlewares/dynamic_domain.rb:17:in `call'
app/middlewares/mongo_mapper_middleware.rb:24:in `call'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
tags_for_autocomplete method in QuestionsController.rb
def tags_for_autocomplete
respond_to do |format|
format.js do
result = []
if q = params[:tag]
result = current_group.questions.find_tags(/^#{Regexp.escape(q.downcase)}/i,
:group_id => current_group.id,
:banned => false)
end
results = result.map do |t|
{:caption => "#{t["name"]} (#{t["count"].to_i})", :value => t["name"]}
end
# if no results, show default tags
if results.empty?
results = current_group.default_tags.map {|tag|{:value=> tag, :caption => tag}}
end
render :json => results
end
end
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/proxy.rb:109
protected
def method_missing(method, *args, &block)
if load_target
target.send(method, *args, &block)
end
end
Replace "current_group.questions.find_tags" with "Question.find_tags" in the 2 locations in code.
As mentioned here:
http://shapado.com/questions/what-is-the-cause-of-the-nomethoderror-when-using-the-search-box-on-the-tags-page
This seems to work for me.
Related
I started to receive the following error in production.
RuntimeError: Circular dependency detected while autoloading constant Ability
from active_support/dependencies.rb:492:in `load_missing_constant'
from active_support/dependencies.rb:184:in `const_missing'
from active_support/dependencies.rb:526:in `load_missing_constant'
from active_support/dependencies.rb:184:in `const_missing'
from active_support/dependencies.rb:526:in `load_missing_constant'
from active_support/dependencies.rb:184:in `const_missing'
from app/controllers/frontend/application_controller.rb:126:in `current_ability'
from cancan/controller_additions.rb:352:in `authorize!'
from app/controllers/documentable_count_controller.rb:5:in `index'
from action_controller/metal/implicit_render.rb:4:in `send_action'
from abstract_controller/base.rb:198:in `process_action'
from action_controller/metal/rendering.rb:10:in `process_action'
from abstract_controller/callbacks.rb:20:in `block in process_action'
from active_support/callbacks.rb:117:in `call'
from active_support/callbacks.rb:117:in `call'
from active_support/callbacks.rb:555:in `block (2 levels) in compile'
from active_support/callbacks.rb:505:in `call'
from active_support/callbacks.rb:505:in `call'
from active_support/callbacks.rb:92:in `__run_callbacks__'
from active_support/callbacks.rb:778:in `_run_process_action_callbacks'
from active_support/callbacks.rb:81:in `run_callbacks'
from abstract_controller/callbacks.rb:19:in `process_action'
from action_controller/metal/rescue.rb:29:in `process_action'
from action_controller/metal/instrumentation.rb:32:in `block in process_action'
from active_support/notifications.rb:164:in `block in instrument'
from active_support/notifications/instrumenter.rb:20:in `instrument'
from active_support/notifications.rb:164:in `instrument'
from action_controller/metal/instrumentation.rb:30:in `process_action'
from action_controller/metal/params_wrapper.rb:250:in `process_action'
from active_record/railties/controller_runtime.rb:18:in `process_action'
from abstract_controller/base.rb:137:in `process'
from action_view/rendering.rb:30:in `process'
from action_controller/metal.rb:196:in `dispatch'
from action_controller/metal/rack_delegation.rb:13:in `dispatch'
from action_controller/metal.rb:237:in `block in action'
from action_dispatch/routing/route_set.rb:74:in `call'
from action_dispatch/routing/route_set.rb:74:in `dispatch'
from action_dispatch/routing/route_set.rb:43:in `serve'
from action_dispatch/journey/router.rb:43:in `block in serve'
from action_dispatch/journey/router.rb:30:in `each'
from action_dispatch/journey/router.rb:30:in `serve'
from action_dispatch/routing/route_set.rb:817:in `call'
from rack/attack.rb:172:in `call'
from warden/manager.rb:36:in `block in call'
from warden/manager.rb:35:in `catch'
from warden/manager.rb:35:in `call'
from rack/etag.rb:24:in `call'
from rack/conditionalget.rb:25:in `call'
from rack/head.rb:13:in `call'
from action_dispatch/middleware/params_parser.rb:27:in `call'
from action_dispatch/middleware/flash.rb:260:in `call'
from rack/session/abstract/id.rb:225:in `context'
from rack/session/abstract/id.rb:220:in `call'
from action_dispatch/middleware/cookies.rb:560:in `call'
from active_record/query_cache.rb:36:in `call'
from active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
from action_dispatch/middleware/callbacks.rb:29:in `block in call'
from active_support/callbacks.rb:88:in `__run_callbacks__'
from active_support/callbacks.rb:778:in `_run_call_callbacks'
from active_support/callbacks.rb:81:in `run_callbacks'
from action_dispatch/middleware/callbacks.rb:27:in `call'
from action_dispatch/middleware/remote_ip.rb:78:in `call'
from action_dispatch/middleware/debug_exceptions.rb:17:in `call'
from action_dispatch/middleware/show_exceptions.rb:30:in `call'
from rails/rack/logger.rb:38:in `call_app'
from rails/rack/logger.rb:20:in `block in call'
from active_support/tagged_logging.rb:68:in `block in tagged'
from active_support/tagged_logging.rb:26:in `tagged'
from active_support/tagged_logging.rb:68:in `tagged'
from rails/rack/logger.rb:20:in `call'
from ahoy/engine.rb:22:in `call_with_quiet_ahoy'
from request_store/middleware.rb:19:in `call'
from action_dispatch/middleware/request_id.rb:21:in `call'
from rack/methodoverride.rb:22:in `call'
from rack/runtime.rb:18:in `call'
from active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
from action_dispatch/middleware/static.rb:120:in `call'
from rack/sendfile.rb:113:in `call'
from rack/cors.rb:97:in `call'
from raven/integrations/rack.rb:50:in `call'
from rails/engine.rb:518:in `call'
from rails/application.rb:165:in `call'
from rack/content_length.rb:15:in `call'
from puma/configuration.rb:225:in `call'
from puma/server.rb:658:in `handle_request'
from puma/server.rb:472:in `process_client'
from puma/server.rb:332:in `block in run'
from puma/thread_pool.rb:133:in `call'
from puma/thread_pool.rb:133:in `block in spawn_thread'
I tried to check if somebody has the problem and some of the posts said to set the "config.eager_load" to true. However, it is already set this way in production.
The problem starts when the code try check if the user is authorized to read a building in the line "authorize! :read, #building".
class DocumentableCountController < ::Frontend::ApplicationController
before_action :redirect_if_no_building
def index
authorize! :read, #building
...
end
...
It calls the following method under the hood where the error occurs which is in the "#current_ability ||= Ability.new(stakeholder)" line.
module Frontend
class ApplicationController < ::ApplicationController
...
def current_ability
stakeholder = current_admin || current_user
#current_ability ||= Ability.new(stakeholder)
end
end
end
It gives the error:
RuntimeError: Circular dependency detected while autoloading constant Ability
It is expected to not have this error.
Extra info
The following code is the "::ApplicationController" class inherited by the Module Frontend above.
class ApplicationController < ActionController::Base
include HasFrontendPartner
include RescuesNotFound
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.html { redirect_to "/403", notice: exception.message }
format.json { render json: { error: exception.message }, status: 403 }
end
end
end
When trying to access aws via RUBY API with a Simple example, i get error:
ruby getAllInstances.rb
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:878:in `initialize': execution expired (Seahorse::Client::NetworkingError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:878:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
here is the ruby script:
#!/usr/bin/env ruby
require 'aws-sdk'
ec2 = Aws::EC2::Resource.new(region: 'us-east-1')
# Get all instances with tag key 'Group'
# and tag value 'MyGroovyGroup':
ec2.instances({filters: [{name: 'tag:op_env', values: ['CI']}]}).each do |i|
puts 'ID: ' + i.id
puts 'State: ' + i.state.name
end
here is environment:
$ more credentials
[default]
aws_access_key_id=####ID####
aws_secret_access_key=######key#####
$ more config
[default]
output = json
region = us-east-1
here is another script, which I am trying to use to create a bucket
#!/usr/bin/env ruby
require 'aws-sdk'
#ec2 = Aws::EC2::Resource.new(region: 'us-east-1')
s3 = Aws::S3::Client.new(region: 'us-east-1')
#s3.create_bucket(bucket: 'remove_me')
Aws.use_bundled_cert!
client = Aws::S3::Client.new(
:region => 'us-east-1',
:access_key_id => 'ID',
:secret_access_key => 'KEY'
)
Aws.config[:ssl_verify_peer] = false
resource = Aws::S3::Resource.new(client:client)
bucket = resource.bucket('digital-evidance-perftest-data')
bucket.objects.each do |obj|
resp = client.get_object(response_target:target, bucket:bucketName, key:obj.key)
end
with the same error:
$ruby create_bucket_s3.rb
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:878:in `initialize': execution expired (Seahorse::Client::NetworkingError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:878:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:877:in `connect'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:857:in `start'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/net_http/connection_pool.rb:285:in `start_session'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/net_http/connection_pool.rb:92:in `session_for'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/net_http/handler.rb:119:in `session'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/net_http/handler.rb:71:in `transmit'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/net_http/handler.rb:45:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/plugins/content_length.rb:12:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_request_signer.rb:88:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_request_signer.rb:23:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/xml/error_handler.rb:8:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/helpful_socket_errors.rb:10:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_request_signer.rb:65:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_redirects.rb:15:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:88:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:119:in `retry_request'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:102:in `retry_if_possible'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:90:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:119:in `retry_request'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:102:in `retry_if_possible'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:90:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:119:in `retry_request'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:102:in `retry_if_possible'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/retry_errors.rb:90:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_dualstack.rb:32:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_accelerate.rb:49:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_md5s.rb:31:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_expect_100_continue.rb:21:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_bucket_name_restrictions.rb:12:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_bucket_dns.rb:31:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/rest/handler.rb:7:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/user_agent.rb:12:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/plugins/endpoint.rb:41:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:40:in `manage_keys'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:32:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/param_validator.rb:21:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/plugins/raise_response_errors.rb:14:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_sse_cpk.rb:19:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_dualstack.rb:24:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_accelerate.rb:34:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/jsonvalue_converter.rb:20:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/idempotency_token.rb:18:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/param_converter.rb:20:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/response_paging.rb:26:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/plugins/response_target.rb:21:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/request.rb:70:in `send_request'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-resources-2.9.11/lib/aws-sdk-resources/request.rb:24:in `call'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-resources-2.9.11/lib/aws-sdk-resources/operations.rb:139:in `all_batches'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-resources-2.9.11/lib/aws-sdk-resources/collection.rb:18:in `each'
from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-resources-2.9.11/lib/aws-sdk-resources/collection.rb:18:in `each'
from create_bucket_s3.rb:19:in `<main>'
This error was due to http proxy not exported in the environment
Once i added proxies i was able to communicate with aws via ruby sdk
I am total newbie to Ruby and Sinatra and I am trying to learn them.
So this is my ruby code (File: ruby_test2.rb)
#!/usr/bin/ruby
require 'rubygems'
class LearnRuby
def initalize
#listen = Hash.new { |hash, key| hash[key] = []; }
end
def fill_listen(uid, mid)
#listen[uid]<<=mid
"In fill_listen #{#listen}\n"
end
end
And this is my Sinatra code (file: sinatra_test2.rb)
require 'sinatra'
require './ruby_test2'
if __FILE__ == $0
lr = LearnRuby.new
end
post '/listen' do
lr.fill_listen params[:uid] , params[:mid]
end
An this is how I am running it.
ruby sinatra_test2.rb <-- Starts the server
curl --data "uid=u1&mid=m312" http://localhost:4567/listen <-- Curl command for post request
When I execute the above I get this error (on server and client(curl) side)
localhost - - [22/Mar/2014:22:54:19 IST] "POST /listen HTTP/1.1" 500 3770
- -> /listen
NoMethodError - undefined method `[]' for nil:NilClass:
<path>/ruby_test2.rb:11:in `fill_listen'
sinatra_test2.rb:9:in `block in <main>'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `block in compile!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `[]'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (3 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:976:in `route_eval'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (2 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:997:in `block in process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:955:in `block in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `each'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1067:in `block in dispatch!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1064:in `dispatch!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `block in call!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `call!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:877:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/xss_header.rb:18:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/path_traversal.rb:16:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/json_csrf.rb:18:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/frame_options.rb:31:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:217:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:210:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/show_exceptions.rb:21:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:180:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:2004:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `block in call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1778:in `synchronize'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
localhost - - [22/Mar/2014:22:54:51 IST] "POST /listen HTTP/1.1" 500 3770
I may be missing out on something small but I need help as I am just starting to learn these technologies.
Thanks in advance.
Here is the working example for you, the #listen was nil, because you misspelled initalize -> initialize
require 'sinatra'
class LearnRuby
def initialize
#listen = {}
end
def fill_listen(uid, mid)
#listen[uid] = mid
"In fill_listen #{ #listen }\n"
end
end
post '/listen' do
lr = LearnRuby.new
lr.fill_listen(params[:uid], params[:mid])
end
replace #listen[uid]<<=mid with #listen[uid] = mid
Also, in case you only require initialize to create an empty hash then you can replace #listen = Hash.new { |hash, key| hash[key] = []; } with #listen = {}
I have a ruby web- / data-server application which is built on a Sinatra foundation. I'm adding EventMachine and em-websocket so that I can have async data transfer connections.
#! /usr/bin/env ruby
require 'yajl'
require 'json'
require 'em-websocket'
require 'sinatra/base'
require 'thin'
require './Actors/DataProvider.rb'
my_host = '10.2.56.87'
my_web_port = '4004'
my_web_sock = '8008'
EventMachine.epoll # choose kernel epoll over select for deterministic execution under load
EventMachine.run do
class SinatraApp < Sinatra::Base
configure do
set :bind, '10.2.56.87'
set :port, '4004'
end
encoder = Yajl::Encoder.new
communicator = DataProvider.new( encoder )
get '/' do
redirect '/index-work.html'
end
before do
content_type 'application/json'
end
post '/auth' do
end
post '/data/:id' do
puts 'got d i '
end
end
EventMachine::WebSocket.start( :host => my_host, :port => my_web_sock ) do | ws |
ws.onopen do
end
ws.onmessage do
end
ws.onclose do
end
end
begin
SinatraApp.run!
rescue => ex
puts "#{ ex.class}: #{ ex.message }"
end
end
The Thin server worked fine when just serving Sinatra, but now that I've added the EM foundation the following occurs:
/usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:192:in `block (2 levels) in start': undefined method `attach_server' for EventMachine:Module (NoMethodError)
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:191:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:191:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:30:in `block (2 levels) in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:23:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker/worker.rb:52:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker/worker.rb:52:in `work'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:129:in `block (2 levels) in spawn_missing_workers'
from (eval):6:in `block in fork'
from (eval):6:in `fork'
from (eval):6:in `fork'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:126:in `block in spawn_missing_workers'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:124:in `times'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:124:in `spawn_missing_workers'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:44:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:37:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:187:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/sinatra-1.3.3/lib/sinatra/base.rb:1350:in `run!'
from /home/devel/ISF_Server/server-work.rb:122:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /home/devel/ISF_Server/server-work.rb:15:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/lib/ruby-debug-ide.rb:127:in `debug_load'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/lib/ruby-debug-ide.rb:127:in `debug_program'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/bin/rdebug-ide:118:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
I do notice that the Thin gem was recently updated, but EM has not been. That's my next point of inquiry. Somebody else posted that Thin should not be run from within EM.run block, but the example I am building from had it this way.
Okay. I corresponded with Marc-Andre Cournoyer directly. He says, first, don't spawn This (through Sinatra) inside an EM.run block.
Secondly, the error is generated from Thin 2 (pre) which requires the EventMachine live Edition, or gem eventmachine-le.
Mo' bettah now. :D
i am trying to parse the following jsons response via activeresource in the padrino framework from a rest-api:
{"result":"Ok","resource":"ProjectRazor::Slice::Model","errcode":0,"command":"models_query_all","response":[{"#uri":"http://192.168.0.50:8026/razor/api/model/5nBqgTngqvxPIxVJJpdeQm","#callback":{},"#version":2,"#uuid":"5nBqgTngqvxPIxVJJpdeQm","#description":"Ubuntu Precise Model","#domainname":"andreasheumaier.de","#counter":0,"#broker_plugin":"agent","#root_password":"/7bbNwa6","#log":[],"#policy_bound":null,"#hostname_prefix":"firebird","#classname":"ProjectRazor::ModelTemplate::UbuntuPrecise","#osversion":"precise","#is_template":false,"#final_state":"os_complete","#node":null,"#req_metadata_hash":{"#domainname":{"validation":"^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9](\\.[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])*$","example":"example.com","required":true,"description":"local domain name (will be used in /etc/hosts file)","default":"localdomain"},"#root_password":{"validation":"^[\\S]{8,}","example":"P#ssword!","required":true,"description":"root password (> 8 characters)","default":"test1234"},"#hostname_prefix":{"validation":"^[a-zA-Z0-9][a-zA-Z0-9\\-]*$","example":"node","required":true,"description":"node hostname prefix (will append node number)","default":"node"}},"#hidden":false,"#name":"ubuntu_precise","#label":"install_precise","#current_state":"init","#result":null,"#template":"linux_deploy","#noun":"model","#image_prefix":"os","#image_uuid":"4VuDj4mJDHP6bvWeztfXOO"}],"http_err_code":200}
I defined this models
class RazorJsonFormatter
include ActiveResource::Formats::JsonFormat
def decode(json)
ActiveResource::Formats::JsonFormat.decode(json)['response']
end
end
class Razor < ActiveResource::Base
include ActiveResource::Formats::JsonFormat
include ActiveResource::Extend::WithoutExtension
self.format = RazorJsonFormatter.new
self.site = "http://yellowbird:8026/razor/api/"
end
class RazorModel < Razor
set_collection_name 'model'
end
This gives following error:
RazorModel.all
NameError: wrong constant name #callback
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1428:in `const_defined?'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1428:in `find_or_create_resource_for'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1278:in `block in load'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1264:in `each'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1264:in `load'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1006:in `initialize'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:933:in `new'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:933:in `instantiate_record'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `block in instantiate_collection'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `collect!'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `instantiate_collection'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:901:in `find_every'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:814:in `find'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:826:in `first'
from (irb):27
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/lib/padrino-core/cli/base.rb:68:in `console'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/task.rb:27:in `run'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:120:in `invoke_task'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor.rb:275:in `dispatch'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:109:in `invoke'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/lib/padrino-core/cli/base.rb:73:in `c'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/task.rb:27:in `run'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:120:in `invoke_task'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor.rb:275:in `dispatch'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/base.rb:425:in `start'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/bin/padrino:9:in `<top (required)>'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/bin/padrino:19:in `load'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/bin/padrino:19:in `<main>'1.9.3p194
(...)
This either works with predifined ActiveResource schema. My current workaround is write all classes from scratch using rest-client
How can parse -or igore- the nested json attributes with activeresource?