I am programming a small rails api for practice. The goal is to POST some data and then save it (after further processing) in a database. The object is being created successfuly. On the save method appears an error which I cannot explain:
NoMethodError (undefined method `[]' for nil:NilClass): app/controllers/api/v1/calculations_controller.rb:19:in `create'
It appears in the :create method (on POST):
# POST /calculations
def create
#calculation = Calculation.new(calculation_params)
if #calculation.save # <- This is line 19
render json: #calculation, status: :created, location: #calculation
else
render json: #calculation.errors, status: :unprocessable_entity
end
end
The model looks like this:
require 'digest'
class Calculation < ApplicationRecord
attr_reader :value, :hash_value, :algorithm, :timestamp
##known_algorithms = ['SHA2_256', 'SHA2_384', 'SHA2_512']
def initialize (params)
#value = params[:value]
#algorithm = params[:algorithm]
validate!
#hash_value, #timestamp = digest
end
private
def digest
case #algorithm
when 'SHA2_256'
result = Digest::SHA2.hexdigest(#value)
when 'SHA2_384'
result = Digest::SHA2.new(384).hexdigest(#value)
when 'SHA2_512'
result = Digest::SHA2.new(512).hexdigest(#value)
end
return result, DateTime.now
end
def validate!
raise ArgumentError.new('Value cannot be empty or NIL!') if #value.nil? or #value.empty?
raise ArgumentError.new('Unknown hashing algorithm!') unless ##known_algorithms.include? #algorithm
end
end
Why does the NoMethodError appear? Did I override some important part in the model class?
Full stack trace as requested:
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:424:in `clear_transaction_record_state'
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:330:in `ensure in rollback_active_record_state!'
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:330:in `rollback_active_record_state!'
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:309:in `save'
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/suppressor.rb:44:in `save'
/home/pbz/RubymineProjects/hasher_api/app/controllers/api/v1/calculations_controller.rb:20:in `create'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/base.rb:194:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/rendering.rb:30:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:132:in `run_callbacks'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/callbacks.rb:41:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/rescue.rb:22:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:168:in `block in instrument'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:168:in `instrument'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/base.rb:134:in `process'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal.rb:191:in `dispatch'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal.rb:252:in `dispatch'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:34:in `serve'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/journey/router.rb:52:in `block in serve'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/journey/router.rb:35:in `each'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/journey/router.rb:35:in `serve'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:840:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/rack-2.0.6/lib/rack/etag.rb:25:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/rack-2.0.6/lib/rack/conditional_get.rb:38:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/rack-2.0.6/lib/rack/head.rb:12:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/migration.rb:559:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:98:in `run_callbacks'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/executor.rb:14:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/railties-5.2.2/lib/rails/rack/logger.rb:38:in `call_app'
/home/pbz/.gem/ruby/2.5.0/gems/railties-5.2.2/lib/rails/rack/logger.rb:26:in `block in call'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:71:in `block in tagged'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:28:in `tagged'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:71:in `tagged'
/home/pbz/.gem/ruby/2.5.0/gems/railties-5.2.2/lib/rails/rack/logger.rb:26:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/rack-2.0.6/lib/rack/runtime.rb:22:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/executor.rb:14:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/static.rb:127:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/rack-2.0.6/lib/rack/sendfile.rb:111:in `call'
/home/pbz/.gem/ruby/2.5.0/gems/railties-5.2.2/lib/rails/engine.rb:524:in `call'
/home/pbz/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
/home/pbz/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
/home/pbz/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
/home/pbz/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
/home/pbz/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'
Don't override initialize in the AR. It could possibly break a lot of stuff in your models.
You can use after_initialize.
The after_initialize callback will be called whenever an Active Record
object is instantiated, either by directly using new or when a record
is loaded from the database. It can be useful to avoid the need to
directly override your Active Record initialize method.
def after_initialize
# Gets called right after Calculation.new
# Do some stuff here
end
If you still want to use initialize you must do something like this:
def initialize(attributes = {})
super
self.attributes = attributes
end
I did find out (thanks to #Зелёный), that to create values for the model after creating it (new) I need to use the after_initialize callback Rails provides. Together with this comment on how to access model instance variables, I was able to achieve the functionality I wanted.
The original NoMethodError was a result of my incorrect use of the initialize method. The following code is the final result, which works with the default controller.
require 'digest'
class Calculation < ApplicationRecord
##known_algorithms = ['SHA2_256', 'SHA2_384', 'SHA2_512']
after_initialize :init
private
def init
validate!
calculate!
end
def calculate!
self.hash_value, self.timestamp = digest
end
def digest
case algorithm
when 'SHA2_256'
result = Digest::SHA2.hexdigest(value)
when 'SHA2_384'
result = Digest::SHA2.new(384).hexdigest(value)
when 'SHA2_512'
result = Digest::SHA2.new(512).hexdigest(value)
end
return result, DateTime.now
end
def validate!
raise ArgumentError.new('Value cannot be empty or NIL!') if value.nil? or value.empty?
raise ArgumentError.new('Unknown hashing algorithm!') unless ##known_algorithms.include? algorithm
end
end
Related
I am trying to customize my 404/422/500 error pages and for some reason I get a blank page with a "500 Internal Server Error". However, in another project the code below works perfectly fine. I hope someone can help me out.
Here are my codes :
Application.rb
module Realbeez
class Application < Rails::Application
config.generators do |generate|
generate.assets false
generate.helper false
generate.test_framework :test_unit, fixture: false
# For error message customizing
config.exceptions_app = self.routes
end
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
end
end
My routes:
get '/404', to: "errors#not_found"
get '/422', to: "errors#unacceptable"
get '/500', to: "errors#internal_error"
My controller ErrorsController:
class ErrorsController < ApplicationController
def not_found
respond_to do |format|
format.html { render status: 404 }
end
end
def unacceptable
respond_to do |format|
format.html { render status: 422 }
end
end
def internal_error
respond_to do |format|
format.html { render status: 500 }
end
end
end
I get the following message in the rails server maybe it can help :
Error during failsafe response: ErrorsController
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/pundit-2.1.0/lib/pundit.rb:192:in `verify_authorized'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:426:in `block in make_lambda'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:236:in `block in halting_and_conditional'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `block in invoke_after'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `each'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `invoke_after'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:133:in `run_callbacks'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/callbacks.rb:41:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/rescue.rb:22:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:168:in `block in instrument'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:168:in `instrument'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:134:in `process'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionview-5.2.3/lib/action_view/rendering.rb:32:in `process'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal.rb:191:in `dispatch'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal.rb:252:in `dispatch'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:34:in `serve'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:52:in `block in serve'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:35:in `each'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:35:in `serve'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:840:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:51:in `render_exception'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:36:in `rescue in call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:38:in `call_app'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `block in call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:71:in `block in tagged'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:28:in `tagged'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:71:in `tagged'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/quiet_assets.rb:13:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/executor.rb:14:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/static.rb:127:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/webpacker-4.0.7/lib/webpacker/dev_server_proxy.rb:29:in `perform_request'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:524:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/configuration.rb:228:in `call'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/server.rb:664:in `handle_request'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/server.rb:467:in `process_client'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/server.rb:328:in `block in run'
/Users/steven/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.1.0/lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Thanks
You can just customize the html error pages in the "public" folder. Did you try it ?
I'm building a Rails shopping cart app using Redis. Everything works fine until I try to view my cart, at which point I get this error.
NoMethodError in CartsController#show
undefined method `SMEMBERS' for nil:NilClass
Extracted source (around line #6):
5 def show
6 cart_ids = $redis.SMEMBERS current_user_cart
7 #order_items = current_order.order_items
8 end
The beginning of the full trace:
app/controllers/carts_controller.rb:6:in `show'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
...and thence through a bunch more behind-the-scenes stuff I haven't touched, mostly activesupport, activerecord, and railties.
Redis-server is installed and responsive; I have tested it with redis-cli.
Config/initializers/redis.rb:
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:url => uri)
App/controllers/carts_controller.rb:
class CartsController < ApplicationController
before_action :authenticate_user!
def show
cart_ids = $redis.SMEMBERS current_user_cart
#order_items = current_order.order_items
end
def add
$redis.SADD current_user_cart, params[:product_id]
render json: current_user.cart_count, status: 200
end
def remove
$redis.SREM current_user_cart, params[:product_id]
render json: current_user.cart_count, status: 200
end
private
def current_user_cart
"cart#{current_user.id}"
end
end
The worst part is that I had this problem last week, fixed it, and neglected to write down what the fix was! Anybody have any leads?
You are using uninitialized global variable $redis (look at your initializers/redis.rb where you declared constant REDIS but not $redis)
Ruby is case-sensitive language, the SMEMBERS and smembers are two different methods. So, I think you should use smembers method.
Turned out I had two versions of redis installed. I don't know if that was causing the problem, but when I uninstalled both and reinstalled the gem, the problem disappeared!
I am stuck in an issue, wherein I would like to send mail after the after_save callback in rails.
Below is my code snippet:
class Response < ActiveRecord::Base
after_create :pending_with
protected
def pending_with
self.approver = self.mapping.user
end
end
class ResponsesController < ApplicationController
def create
#response = current_user.responses.new(response_params)
respond_to do |format|
if #response.save
flash[:notice] = "Response has been saved successfully."
Notification.confirm_approver(#response).deliver
format.html { redirect_to dashboard_home_url }
else
flash[:error] = "There is some problem while saving the responses. Please try again."
format.html { render action: 'new' }
end
end
end
end
class Notification < ApplicationMailer
def confirm_approver(response)
#response = response
p "============"
p #response.approver.email.inspect
end
end
My issue is that, I am not getting approver. Getting error:
undefined method `approver' for nil:NilClass
When I checked the record in the database, apporver ('id') is successfully saved. Am I missing anything here?
BACKTRACE:
app/mailers/notification.rb:11:in `confirm_approver'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
vendor/gems/ruby/1.9.1actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:580:in `block in process'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:577:in `process'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:568:in `initialize'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:551:in `new'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:551:in `method_missing'
app/controllers/responses_controller.rb:23:in `block in create'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:433:in `call'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:433:in `retrieve_collector_from_mimes'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/responses_controller.rb:19:in `create'
Hi Based on #zwippie inputs, it worked. Just made little modification to it.
def create
#response = current_user.responses.new(response_params)
respond_to do |format|
if #response.save && #response.reload
Notification.confirm_approver(#response).deliver
flash[:notice] = "Response has been created successfully."
else
format.html { render action: 'new' }
end
end
end
I have a very strange problem which I am not able to tackle. Two Rails apps, one is an API that has an entity Mongo::Enrollment, and the other one is a client that has the entity Enrollment connected to the API with ActiveResource.
I defined my /app/models/enrollment.rb on the client with ActiveResource, and in the controller I am making a call like this:
enrollment = Enrollment.post(:create, enrollment: {
:enrollment_type => :test,
:enrollment_information => {test: 'test information'}
})
I know I can use .new and .save here too as it is ActiveResource, but it is giving me the same results.
When I actually post this, I get the following error in my log:
RuntimeError (Circular dependency detected while autoloading constant Enrollment)
But if I copy the URL from my log, paste it in Postman using the same Basic auth, accept header and POST method, I get no error at all and it saves the enrollment into mongodb.
I really hope someone can help me. I think it's very strange that what seems to be the exact same HTTP request, fetches different results. I have tried restarting both apps, and doing the request in different manners from the client, but every time I make the request from the client it's giving me the error above. The controller and the model on the API side seem to be fine, I also have an HTML interface on the API app and that's working perfectly.
This is what I have in my API app:
It is defined in routes simple as:
resources :enrollments
In the EnrollmentsController I have
# /app/controllers/enrollments_controller.rb
class EnrollmentsController < ApplicationController
...
def create
#enrollment = Mongo::Enrollment.new(enrollment_parameters)
respond_to do |format|
if #enrollment.save
format.html { redirect_to #enrollment, notice: 'Note was successfully created.' }
format.json { render json: #enrollment }
else
format.html { render action: 'new' }
format.json { render json: #enrollment.errors, status: :unprocessable_entity }
end
end
end
...
end
This is my model:
# /app/models/mongo/enrollment.rb
module Mongo
class Enrollment
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
default_scope ->{ where(processed: false)}
field :processed, :type => Boolean, :default => false
field :accepted, :type => Boolean, :default => false
field :enrollment_type
field :enrollment_information, :type => Hash, :default => {}
end
end
Full stack trace:
Started POST "/api/v1.0/enrollments/create.json?enrollment..." for 127.0.0.1 at 2014-08-31 10:12:33 -0400
RuntimeError (Circular dependency detected while autoloading constant Enrollment):
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:461:in `load_missing_constant'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:184:in `const_missing'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `const_get'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `block in constantize'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `each'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `inject'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `constantize'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:269:in `safe_constantize'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/core_ext/string/inflections.rb:77:in `safe_constantize'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:145:in `_default_wrap_model'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:96:in `block in model'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/mutex_m.rb:73:in `mu_synchronize'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:96:in `model'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:119:in `name'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:252:in `_wrapper_key'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:275:in `_wrapper_enabled?'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:233:in `process_action'
vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/abstract_controller/base.rb:136:in `process'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/abstract_controller/rendering.rb:44:in `process'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal.rb:195:in `dispatch'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal.rb:231:in `block in action'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:80:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:48:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/journey/router.rb:71:in `block in call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/journey/router.rb:59:in `each'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/journey/router.rb:59:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:680:in `call'
vendor/bundle/ruby/2.1.0/gems/warden-1.2.3/lib/warden/manager.rb:35:in `block in call'
vendor/bundle/ruby/2.1.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `catch'
vendor/bundle/ruby/2.1.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/etag.rb:23:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/conditionalget.rb:35:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/flash.rb:241:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/cookies.rb:486:in `call'
vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/query_cache.rb:36:in `call'
vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:369:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__928559686495229391__call__callbacks'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/reloader.rb:64:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/rack/logger.rb:38:in `call_app'
vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/rack/logger.rb:20:in `block in call'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/tagged_logging.rb:67:in `block in tagged'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/tagged_logging.rb:25:in `tagged'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/tagged_logging.rb:67:in `tagged'
vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/rack/logger.rb:20:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/request_id.rb:21:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/static.rb:64:in `call'
vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call'
vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/engine.rb:511:in `call'
vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application.rb:97:in `call'
/usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
/usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
/usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
/usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'
Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.8ms)
Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (20.3ms)
Thank you!
It sounds like the wrap_parameters code is confused by the fact that your model is namespaced inside the Mongo module.
It also sounds like you don't actually need parameter wrapping so the easiest thing is to disable it - there should be a wrap_parameters initializer. You can re-enable it on a controller by controller basis if you need to.
I have same class in main application and a gem (mountable engine).
I want to reopen the class(defined in main applicaion) in the gem.
Main application has
app/models/test.rb
class Test
def original_method
end
end
in Gemfile
gem 'gem_name'
In the gem
app/models/test.rb
class Test
def add_method
end
end
But main application raises an error that Test#original_method is not defined.
It looks like Test class is overwritten in the gem. Why not reopen?
How can I solve the problem??
EDIT
NoMethodError (undefined method `original_method' for #<Test:0x0000001997ca70>):
vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
vendor/bundle/ruby/1.9.1/bundler/gems/test_gem/app/controllers/tests_controller.rb:213:in `new'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:167:in `process_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/rendering.rb:10:in `process_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:535:in `_run__422315203817736318__process_action__439125674649202736__callbacks'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:in `process_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in `process_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:121:in `process'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in `process'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal.rb:203:in `dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_controller/metal.rb:246:in `block in action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:in `call'
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:in `call'
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in `catch'
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/etag.rb:23:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/conditionalget.rb:25:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/head.rb:14:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/flash.rb:242:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/cookies.rb:341:in `call'
vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/query_cache.rb:64:in `call'
vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `_run__4226710667851682500__call__4227166089575601802__callbacks'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/reloader.rb:65:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/rack/logger.rb:32:in `call_app'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/rack/logger.rb:16:in `block in call'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/tagged_logging.rb:22:in `tagged'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/rack/logger.rb:16:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/request_id.rb:22:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/runtime.rb:17:in `call'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/lock.rb:15:in `call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/static.rb:63:in `call'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/engine.rb:479:in `call'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:223:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/content_length.rb:14:in `call'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/rack/log_tailer.rb:17:in `call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
try
Test.class_eval do
def original_method
end
end
This is an old question, but I just came across it looking for an answer and found my own. Here's how I did it in a Rails app. Basically, instead of reopening the class you put the code in a module and include it in the original class.
# lib/extensions/gem_name/name_of_feature.rb
module Extensions
module GemName
module NameOfFeature
def self.included(base)
base.extend ClassMethods
base.class_eval do
# Things like association macros here
# I.ex. belongs_to :foo
end
end
def some_instance_method
# Do something
end
module ClassMethods
def some_class_method
# Do something
end
end
end
end
end
# config/initializers/gem_name.rb
require "extensions/gem_name/name_of_feature"
GemClassToExtend.include Extensions::GemName::NameOfFeature
So now you can:
GemClassToExtend.some_class_method
GemClassToExtend.new.some_instance_method
You have two test.rb files and only one of them is being loaded in your current environment. I recommend creating a mixin module in your application and then including it into the Test class defined in the gem.
module TestExtensions
def original_method
end
end
Test.include(TestExtensions)
Aside: I typically discourage monkey patching gem dependencies in this manner. Consider making a wrapper around Test.
The exception you have attached claims about undefined method original_method? not original_method. Looks like you are trying to call a method with ? at the end while it is not defined.