Does not pass authorization (devise auth token) - ruby-on-rails

I am using Devise auth token gem for authenticating some parts of my rails app. But when I try to log in, I get the following error:
POST http://localhost:3000/auth/sign_in 500 (Internal Server Error)
Here is the complete trace:
Started POST "/auth/sign_in" for 10.12.4.5 at 2018-05-30 09:29:36
+0500 Processing by DeviseTokenAuth::SessionsController#create as JSON Parameters: {"login"=>"admin", "password"=>"[FILTERED]",
"session"=>{"login"=>"admin", "password"=>"[FILTERED]"}} Completed 500
Internal Server Error in 14ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `each' for
):
devise_token_auth (0.1.43)
app/controllers/devise_token_auth/application_controller.rb:35:in
params_for_resource' devise_token_auth (0.1.43)
app/controllers/devise_token_auth/sessions_controller.rb:129:in
resource_params' devise_token_auth (0.1.43)
app/controllers/devise_token_auth/sessions_controller.rb:13:in
create' actionpack (5.1.6)
lib/action_controller/metal/basic_implicit_render.rb:4:in
send_action' actionpack (5.1.6)
lib/abstract_controller/base.rb:186:in process_action' actionpack
(5.1.6) lib/action_controller/metal/rendering.rb:30:in
process_action' actionpack (5.1.6)
lib/abstract_controller/callbacks.rb:20:in block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in
run_callbacks' actionpack (5.1.6)
lib/abstract_controller/callbacks.rb:19:in process_action' actionpack
(5.1.6) lib/action_controller/metal/rescue.rb:20:inprocess_action'
actionpack (5.1.6)
lib/action_controller/metal/instrumentation.rb:32:in block in
process_action' activesupport (5.1.6)
lib/active_support/notifications.rb:166:inblock in instrument'
activesupport (5.1.6)
lib/active_support/notifications/instrumenter.rb:21:in instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in
instrument' actionpack (5.1.6)
lib/action_controller/metal/instrumentation.rb:30:in process_action'
actionpack (5.1.6)
lib/action_controller/metal/params_wrapper.rb:252:inprocess_action'
activerecord (5.1.6)
lib/active_record/railties/controller_runtime.rb:22:in
process_action' actionpack (5.1.6)
lib/abstract_controller/base.rb:124:inprocess' actionpack (5.1.6)
lib/action_controller/metal.rb:189:in dispatch' actionpack (5.1.6)
lib/action_controller/metal.rb:253:indispatch' actionpack (5.1.6)
lib/action_dispatch/routing/route_set.rb:49:in dispatch' actionpack
(5.1.6) lib/action_dispatch/routing/route_set.rb:31:inserve'
actionpack (5.1.6) lib/action_dispatch/routing/mapper.rb:16:in block
in <class:Constraints>' actionpack (5.1.6)
lib/action_dispatch/routing/mapper.rb:46:inserve' actionpack (5.1.6)
lib/action_dispatch/journey/router.rb:50:in block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:ineach'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in
call' warden (1.2.7) lib/warden/manager.rb:36:in block in call'
warden (1.2.7) lib/warden/manager.rb:35:incatch' warden (1.2.7)
lib/warden/manager.rb:35:in call' rack (2.0.5) lib/rack/etag.rb:25:in
call' rack (2.0.5) lib/rack/conditional_get.rb:38:in call' rack
(2.0.5) lib/rack/head.rb:12:incall' activerecord (5.1.6)
lib/active_record/migration.rb:556:in call' actionpack (5.1.6)
lib/action_dispatch/middleware/callbacks.rb:26:inblock in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in
run_callbacks' actionpack (5.1.6)
lib/action_dispatch/middleware/callbacks.rb:24:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/executor.rb:12:in call'
actionpack (5.1.6)
lib/action_dispatch/middleware/debug_exceptions.rb:59:incall'
actionpack (5.1.6)
lib/action_dispatch/middleware/show_exceptions.rb:31:in call'
railties (5.1.6) lib/rails/rack/logger.rb:36:incall_app' railties
(5.1.6) lib/rails/rack/logger.rb:24:in block in call' activesupport
(5.1.6) lib/active_support/tagged_logging.rb:69:inblock in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in
tagged' activesupport (5.1.6)
lib/active_support/tagged_logging.rb:69:intagged' railties (5.1.6)
lib/rails/rack/logger.rb:24:in call' actionpack (5.1.6)
lib/action_dispatch/middleware/remote_ip.rb:79:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in call' rack
(2.0.5) lib/rack/runtime.rb:22:incall' activesupport (5.1.6)
lib/active_support/cache/strategy/local_cache_middleware.rb:27:in
call' actionpack (5.1.6)
lib/action_dispatch/middleware/executor.rb:12:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/static.rb:125:in call' rack
(2.0.5) lib/rack/sendfile.rb:111:incall' rack-cors (1.0.2)
lib/rack/cors.rb:97:in call' railties (5.1.6)
lib/rails/engine.rb:522:incall' puma (3.11.4)
lib/puma/configuration.rb:225:in call' puma (3.11.4)
lib/puma/server.rb:632:inhandle_request' puma (3.11.4)
lib/puma/server.rb:446:in process_client' puma (3.11.4)
lib/puma/server.rb:306:inblock in run' puma (3.11.4)
lib/puma/thread_pool.rb:120:in `block in spawn_thread'
How can I fix the error?
application_controller.rb:
class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:login, :first_name, :patronymic, :last_name, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.permit(:sign_in) { |u| u.permit(:login, :password) }
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:password, :password_confirmation, :current_password) }
end
end
user.rb:
class User < ActiveRecord::Base
self.table_name = 'DASHBOARD.V_L_USERS'
self.primary_key = 'user_id'
devise :database_authenticatable, :validatable, :authentication_keys => [:login]
include DeviseTokenAuth::Concerns::User
has_many :permission_references, as: :source
accepts_nested_attributes_for :permission_references, :allow_destroy => true
has_many :user_roles
has_many :spr_dashboards, foreign_key: 'l_users_id'
accepts_nested_attributes_for :user_roles, :allow_destroy => true
def email_required?
false
end
def password_required?
false
end
def is_participant
(read_attribute(:is_participant) == "Y")
end
def is_participant=(value)
write_attribute(:is_participant, (value == "1") ? "Y" : "N")
end
def children_users_ids
ActiveRecord::Base.connection.select_values <<-SQL.strip_heredoc
SELECT l_users_id_right
FROM DASHBOARD.V_L_USERS_RIGHTS
WHERE l_users_id = #{user_id}
SQL
end
end
routes.rb:
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth'
end

It looks like blocks are not supported in devise_parameter_sanitizer. https://github.com/lynndylanhurley/devise_token_auth/issues/758#issuecomment-280797969

Related

TypeError (superclass mismatch for class SessionsController)

I do authorization through "login" and because of this there is such an error:
Started POST "/auth/sign_in" for 10.12.4.5 at 2018-08-07 10:20:55
+0500
LoadError (Unable to autoload constant Overrides::SessionsController,
expected /home/admin/Рабочий
стол/demo/backend/app/controllers/overrides/sessions_controller.rb to
define it):
activesupport (5.1.6) lib/active_support/dependencies.rb:512:in
load_missing_constant' activesupport (5.1.6)
lib/active_support/dependencies.rb:202:inconst_missing'
activesupport (5.1.6) lib/active_support/inflector/methods.rb:284:in
const_get' activesupport (5.1.6)
lib/active_support/inflector/methods.rb:284:inblock in constantize'
activesupport (5.1.6) lib/active_support/inflector/methods.rb:267:in
each' activesupport (5.1.6)
lib/active_support/inflector/methods.rb:267:ininject' activesupport
(5.1.6) lib/active_support/inflector/methods.rb:267:in constantize'
activesupport (5.1.6) lib/active_support/dependencies.rb:583:inget'
activesupport (5.1.6) lib/active_support/dependencies.rb:614:in
constantize' actionpack (5.1.6)
lib/action_dispatch/http/request.rb:85:incontroller_class_for'
actionpack (5.1.6) lib/action_dispatch/http/parameters.rb:99:in
binary_params_for?' actionpack (5.1.6)
lib/action_dispatch/http/parameters.rb:90:inset_binary_encoding'
actionpack (5.1.6) lib/action_dispatch/http/parameters.rb:67:in
path_parameters=' actionpack (5.1.6)
lib/action_dispatch/journey/router.rb:48:inblock in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in each'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:inserve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in
call' warden (1.2.7) lib/warden/manager.rb:36:inblock in call'
warden (1.2.7) lib/warden/manager.rb:35:in catch' warden (1.2.7)
lib/warden/manager.rb:35:incall' rack (2.0.5) lib/rack/etag.rb:25:in
call' rack (2.0.5) lib/rack/conditional_get.rb:38:incall' rack
(2.0.5) lib/rack/head.rb:12:in call' activerecord (5.1.6)
lib/active_record/migration.rb:556:incall' actionpack (5.1.6)
lib/action_dispatch/middleware/callbacks.rb:26:in block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in
run_callbacks' actionpack (5.1.6)
lib/action_dispatch/middleware/callbacks.rb:24:in call' actionpack
(5.1.6) lib/action_dispatch/middleware/executor.rb:12:incall'
actionpack (5.1.6)
lib/action_dispatch/middleware/debug_exceptions.rb:59:in call'
actionpack (5.1.6)
lib/action_dispatch/middleware/show_exceptions.rb:31:incall'
railties (5.1.6) lib/rails/rack/logger.rb:36:in call_app' railties
(5.1.6) lib/rails/rack/logger.rb:24:inblock in call' activesupport
(5.1.6) lib/active_support/tagged_logging.rb:69:in block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in
tagged' activesupport (5.1.6)
lib/active_support/tagged_logging.rb:69:in tagged' railties (5.1.6)
lib/rails/rack/logger.rb:24:incall' actionpack (5.1.6)
lib/action_dispatch/middleware/remote_ip.rb:79:in call' request_store
(1.4.1) lib/request_store/middleware.rb:19:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in call' rack
(2.0.5) lib/rack/runtime.rb:22:incall' activesupport (5.1.6)
lib/active_support/cache/strategy/local_cache_middleware.rb:27:in
call' actionpack (5.1.6)
lib/action_dispatch/middleware/executor.rb:12:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/static.rb:125:in call' rack
(2.0.5) lib/rack/sendfile.rb:111:incall' rack-cors (1.0.2)
lib/rack/cors.rb:97:in call' railties (5.1.6)
lib/rails/engine.rb:522:incall' puma (3.11.4)
lib/puma/configuration.rb:225:in call' puma (3.11.4)
lib/puma/server.rb:632:inhandle_request' puma (3.11.4)
lib/puma/server.rb:446:in process_client' puma (3.11.4)
lib/puma/server.rb:306:inblock in run' puma (3.11.4)
lib/puma/thread_pool.rb:120:in `block in spawn_thread'
TypeError (superclass mismatch for class SessionsController):
app/controllers/overrides/sessions_controller.rb:1:in `'
/overrides/sessions_controller.rb:
class SessionsController < DeviseTokenAuth::ApplicationController
def resource_data(opts = {})
opts[:resource_json] || #resource.as_json
end
def render_new_error
render json: {
errors: [I18n.t("devise_token_auth.sessions.not_supported")]
}, status: 405
end
def render_create_success
render json: {
sponsor: resource_data(resource_json: #resource.userable.token_validation_response)
}
end
def render_create_error_not_confirmed
render json: {
errors: [I18n.t("devise_token_auth.sessions.not_confirmed", login: #resource.login)]
}, status: 404
end
def render_create_error_bad_credentials
render json: {
errors: [I18n.t("devise_token_auth.sessions.bad_credentials")]
}, status: 402
end
def render_destroy_success
render json: {
}, status: 200
end
def render_destroy_error
render json: {
errors: [I18n.t("devise_token_auth.sessions.user_not_found")]
}, status: 404
end
end
application_controller.rb:
class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
end
routes.rb:
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
sessions: 'overrides/sessions'
}
end
Actually the error message says what needs to be done:
Unable to autoload constant Overrides::SessionsController, expected /home/admin/Рабочий стол/demo/backend/app/controllers/overrides/sessions_controller.rb to define it)
Rails expects app/controllers/overrides/sessions_controller.rb to define Overrides::SessionsController, but can't find it.
Change your controller class definition to:
class Overrides::SessionsController < DeviseTokenAuth::ApplicationController
...
end

Rails 5.1.6 - Stack level too deep

I'm using Rails 5.1.6 with Puma and MySQL.
I have the below code in a Rails controller:
class ScansController < ApplicationController
before_action :find_device, only: %i[index new create]
.
.
.
def create
#scan = #device.scans.create(scan_params)
flash[:notice] = t('.success') if #scan.save
respond_with #scan.device, #scan
end
.
.
.
private
def find_device
#device = Device.find params[:device_id]
end
def scan_params
params.require(:scan).permit(:photo, :device_id, :reading, :reading_date)
end
end
And when I send a POST request to this URL the below happens:
Started POST "/en/devices/1/scans" for 81.0.89.20 at 2018-04-03 09:39:19 +0000
09:39:19 web.1 | Processing by ScansController#create as HTML
09:39:19 web.1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"QFdUs2thGPzJMbcWfApbattFMvnIEfzN1swrVGnbOTx7XbjY0Y3A8PLI1bRxqyBv6xIk9D2t13bfLPAqdSIyzQ==", "scan"=>{"reading_date"=>"2018-04-03T11:39:11", "reading"=>"6"}, "button"=>"", "locale"=>"en", "device_id"=>"1"}
09:39:19 web.1 | User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
09:39:19 web.1 | Device Load (0.6ms) SELECT `devices`.* FROM `devices` WHERE `devices`.`id` = 1 LIMIT 1
09:39:19 web.1 | (0.6ms) BEGIN
09:39:19 web.1 | (0.7ms) ROLLBACK
09:39:19 web.1 | Completed 500 Internal Server Error in 33ms (ActiveRecord: 2.9ms)
09:39:19 web.1 |
09:39:19 web.1 |
09:39:19 web.1 |
09:39:19 web.1 | SystemStackError (stack level too deep):
09:39:19 web.1 |
09:39:19 web.1 | app/controllers/scans_controller.rb:35:in `create'
Now, line 35 is this line:#scan = #device.scans.create(scan_params)
and this was working well in Rails 4, but now that I migrated to Rails 5.1.6 it produces this behaviior even in my integration tests.
I hav no clue what could be the reason for this behavior. Does anyone sees something my eyes avoided for the last one or two weeks?
Thanx in advance.
The models:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Scan < ApplicationRecord
.
.
belongs_to :device, inverse_of: :scans
validates_attachment(
:photo,
content_type: {
content_type: [
'image/jpeg',
'image/gif',
'image/png',
'image/tiff'
]
}
)
with_options unless: #photo.present? do
validates :reading, presence: true
validates :reading, numericality: true
validates :reading_date, presence: true
end
.
end
class Device < ApplicationRecord
.
.
has_many :scans, inverse_of: :device, dependent: :restrict_with_error
validates :unit, presence: true
validates :name, presence: true
validates :serial, presence: true, uniqueness: true
validates :owner, presence: true
validates :precision, numericality: {
only_integer: true,
greater_than_or_equal_to: 0
}, allow_nil: true
.
.
end
The Full stack trace looks like this:
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.6) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations.rb:335:in `valid?'
activerecord (5.1.6) lib/active_record/validations.rb:65:in `valid?'
activesupport (5.1.6) lib/active_support/callbacks.rb:422:in `block in inverted_lambda'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `block (2 levels) in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `each'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `all?'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `block in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:130:in `run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.6) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations.rb:335:in `valid?'
activerecord (5.1.6) lib/active_record/validations.rb:65:in `valid?'
activerecord (5.1.6) lib/active_record/validations.rb:82:in `perform_validations'
activerecord (5.1.6) lib/active_record/validations.rb:44:in `save'
activerecord (5.1.6) lib/active_record/attribute_methods/dirty.rb:35:in `save'
activerecord (5.1.6) lib/active_record/transactions.rb:308:in `block (2 levels) in save'
activerecord (5.1.6) lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
activerecord (5.1.6) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.6) lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
activerecord (5.1.6) lib/active_record/transactions.rb:308:in `block in save'
activerecord (5.1.6) lib/active_record/transactions.rb:323:in `rollback_active_record_state!'
activerecord (5.1.6) lib/active_record/transactions.rb:307:in `save'
activerecord (5.1.6) lib/active_record/suppressor.rb:42:in `save'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/active_record_prepend.rb:16:in `block in save'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/transaction.rb:917:in `with_database_metric_name'
newrelic_rpm (5.0.0.342) lib/new_relic/agent.rb:618:in `with_database_metric_name'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/active_record_prepend.rb:15:in `save'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:371:in `insert_record'
activerecord (5.1.6) lib/active_record/associations/has_many_association.rb:34:in `insert_record'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:360:in `block (2 levels) in _create_record'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:447:in `replace_on_target'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:281:in `add_to_target'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:358:in `block in _create_record'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:129:in `block in transaction'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
activerecord (5.1.6) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:128:in `transaction'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:357:in `_create_record'
activerecord (5.1.6) lib/active_record/associations/has_many_association.rb:121:in `_create_record'
activerecord (5.1.6) lib/active_record/associations/association.rb:196:in `create'
activerecord (5.1.6) lib/active_record/associations/collection_proxy.rb:347:in `create'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:108:in `block in run_callbacks'
activesupport (5.1.6) lib/active_support/core_ext/time/zones.rb:64:in `use_zone'
browser-timezone-rails (1.0.1) lib/browser-timezone-rails.rb:15:in `set_time_zone'
activesupport (5.1.6) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rollbar (2.15.5) lib/rollbar/middleware/rails/rollbar.rb:24:in `block in call'
rollbar (2.15.5) lib/rollbar.rb:145:in `scoped'
rollbar (2.15.5) lib/rollbar/middleware/rails/rollbar.rb:22:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
rollbar (2.15.5) lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.1.6) lib/rails/engine.rb:522:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
This is the part which is repeating in the stack trace:
activesupport (5.1.6) lib/active_support/callbacks.rb:422:in 'block in inverted_lambda'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'block (2 levels) in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'each'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'all?'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'block in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'block in invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'each'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:130:in 'run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in '_run_validate_callbacks'
activemodel (5.1.6) lib/active_model/validations.rb:405:in 'run_validations!'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in 'block in run_validations!'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in 'run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in '_run_validation_callbacks'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in 'run_validations!'
activemodel (5.1.6) lib/active_model/validations.rb:335:in 'valid?'
activerecord (5.1.6) lib/active_record/validations.rb:65:in 'valid?'
It's a race condition.
I think you can only have 1 inverse_of since scan will call device and vice versa.
I've got the solution, although I don't exactly understand why it was working till now.
Originally there was the below validation in the Scan model (as I posted earlier):
with_options unless: #photo.present? do
validates :reading, presence: true
validates :reading, numericality: true
validates :reading_date, presence: true
end
In irb I managed to localize the error to this validation part. After refactoring the #photo.present? to a new method and calling that method in the validation, the problem is solved:
with_options unless: :has_photo? do
validates :reading, presence: true
validates :reading, numericality: true
validates :reading_date, presence: true
end
def has_photo?
#photo.present?
end
According to the document:
:unless option:
Specifies a method, proc or string to call to determine if
the validation should not occur. The method, proc
or string should return or evaluate to a true or false value.
But, your model Scan have unless: #photo.present?:
with_options unless: #photo.present? do
validates :reading, presence: true
...
end
That will lead to unexpected exceptions. In order to solve this issue, we should use any one of them below:
# Option 1:
with_options unless: proc { |scan| scan.photo.present? }
# Option 2:
with_options unless: 'photo.present?'
# Option 3:
with_options unless: :photo_present?
private
def photo_present?
photo.present?
end
In my case I encountered a similar error due to bad naming, please consider checking if the names could interfere with something inside of rails

Rails Engine - NameError: uninitialized constant

I'm creating a Rails Engine Classification, in which I have the Class Passion and a Concern Passionable.
I have also another engine Post, where I include the Passionable Concern. This Passionable Concern belongs to the Engine Classification in the /lib folder.
Turns out that, whenever I try to do a new Post passing the passion_ids, I get this error message:
NameError (uninitialized constant Feed::Post::Categorization):
"Categorization" is the jointable that I created in Engine
Classification.
The piece of code is posted down below.
Engine Classification
# passion.rb
module Classification
class Passion < ApplicationRecord
...
has_many :categorizations
has_many :posts, as: :categorizable, through: :categorizations
...
end
end
#==========================================//
# Classification.rb (JoinTable)
module Classification
class Categorization < ApplicationRecord
belongs_to :passion
belongs_to :categorizable, polymorphic: true
end
end
#===========================================//
# classification/lib/passionable.rb
require 'active_support/concern'
module Classification
module Passionable extend ActiveSupport::Concern
included do
has_many :categorizations, as: :categorizable
has_many :passions, through: :categorizations
end
end
end
Engine Post
module Feed
class Post < ApplicationRecord
has_many :pictures, class_name: "Feed::Picture"
include ::Classification::Passionable
...
end
end
Json to create post.
{
"post": {
"title": "My post title",
"content": "Lorem ipsum dolor sit amet..",
"passion_ids":["8bf8559a-857d-4ca5-82e1-f93dd3effc35","d51747b7-0960-460a-b15a-4b593155e2c5"]
}
}
ERROR MESSAGE:
NameError (uninitialized constant Feed::Post::Categorization):
activerecord (5.0.2) lib/active_record/inheritance.rb:152:in `compute_type'
activerecord (5.0.2) lib/active_record/reflection.rb:354:in `compute_class'
activerecord (5.0.2) lib/active_record/reflection.rb:350:in `klass'
activerecord (5.0.2) lib/active_record/reflection.rb:745:in `source_reflection'
activerecord (5.0.2) lib/active_record/reflection.rb:925:in `check_validity!'
activerecord (5.0.2) lib/active_record/associations/association.rb:25:in `initialize'
activerecord (5.0.2) lib/active_record/associations/has_many_through_association.rb:8:in `initialize'
activerecord (5.0.2) lib/active_record/associations.rb:240:in `new'
activerecord (5.0.2) lib/active_record/associations.rb:240:in `association'
activerecord (5.0.2) lib/active_record/associations/builder/collection_association.rb:66:in `passion_ids='
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:46:in `public_send'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:46:in `_assign_attribute'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:39:in `each'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
activerecord (5.0.2) lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
activerecord (5.0.2) lib/active_record/core.rb:319:in `initialize'
activerecord (5.0.2) lib/active_record/inheritance.rb:65:in `new'
activerecord (5.0.2) lib/active_record/inheritance.rb:65:in `new'
engines/feed/app/controllers/feed/posts_controller.rb:21:in `create'
actionpack (5.0.2) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.2) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.2) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.2) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.2) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.2) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.2) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.2) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.2) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.2) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.2) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.2) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.2) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.2) lib/abstract_controller/base.rb:126:in `process'
actionpack (5.0.2) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.2) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:725:in `call'
railties (5.0.2) lib/rails/engine.rb:522:in `call'
railties (5.0.2) lib/rails/railtie.rb:193:in `public_send'
railties (5.0.2) lib/rails/railtie.rb:193:in `method_missing'
actionpack (5.0.2) lib/action_dispatch/routing/mapper.rb:17:in `block in <class:Constraints>'
actionpack (5.0.2) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
activerecord (5.0.2) lib/active_record/migration.rb:553:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.2) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.2) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.2) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.2) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.2) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.2) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.2) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.2) lib/rails/engine.rb:522:in `call'
puma (3.8.2) lib/puma/configuration.rb:224:in `call'
puma (3.8.2) lib/puma/server.rb:600:in `handle_request'
puma (3.8.2) lib/puma/server.rb:435:in `process_client'
puma (3.8.2) lib/puma/server.rb:299:in `block in run'
puma (3.8.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
Your concern needs to provide a class_name option for the join table model:
# classification/lib/passionable.rb
require 'active_support/concern'
module Classification
module Passionable extend ActiveSupport::Concern
included do
has_many :categorizations, as: :categorizable, class_name: 'Classification::Categorization'
has_many :passions, through: :categorizations
end
end
end
Because your model and concern are under different namespaces.

ActiveModel::ForbiddenAttributesError in Admin::MerchantsController#create

I am getting ForbiddenAttrutesError every time I try and save the below form
.mdl-grid.mdl-cell.mdl-cell--6-col.mdl-cell--4-offset
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= form.text_field :name, class: 'mdl-textfield__input'
= form.label :name, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= form.email_field :email, class: 'mdl-textfield__input'
= form.label :email, class: 'mdl-textfield__label'
= form.fields_for :addresses, Address.new do |address_fields|
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :first_name, class: 'mdl-textfield__input'
= address_fields.label :first_name, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :last_name, class: 'mdl-textfield__input'
= address_fields.label :last_name, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :address1, class: 'mdl-textfield__input'
= address_fields.label :address1, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :address2, class: 'mdl-textfield__input'
= address_fields.label :address2, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :city, class: 'mdl-textfield__input'
= address_fields.label :city, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :zip_code, class: 'mdl-textfield__input'
= address_fields.label :zip_code, class: 'mdl-textfield__label'
Address Model
class Address < ActiveRecord::Base
belongs_to :state
belongs_to :country
belongs_to :address_type
belongs_to :addressable, :polymorphic => true
Merchant Model
has_many :addresses, dependent: :destroy, as: :addressable
has_one :default_billing_address, -> { where(billing_default: true, active: true) },
as: :addressable,
class_name: 'Address'
has_many :billing_addresses, -> { where(active: true) },
as: :addressable,
class_name: 'Address'
has_one :default_shipping_address, -> { where(default: true, active: true) },
as: :addressable,
class_name: 'Address'
has_many :shipping_addresses, -> { where(active: true) },
as: :addressable,
class_name: 'Address'
before_validation :sanitize_data
validates :name, presence: true, length: { maximum: 255 }
validates :email, format: { with: CustomValidators::Emails.email_validator }, :length => { :maximum => 255 }
# geocoded_by :address
# after_validation :geocode
# after_create :sanitize_dates
accepts_nested_attributes_for :addresses
Merchant Controller
private
def allowed_params
params.require(:merchant).permit(:name, :email, addresses_attributes: [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id])
end
Full Error Message
Started POST "/en/admin/merchants" for 127.0.0.1 at 2016-05-24 22:30:02 +1000
Processing by Admin::MerchantsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "merchant"=>{"name"=>"Test", "email"=>"test#test.com", "addresses_attributes"=>{"0"=>{"first_name"=>"Paul", "last_name"=>"M", "address1"=>"12 Dansu Ct", "address2"=>"", "city"=>"Hallam", "zip_code"=>"3803"}}}, "commit"=>"Create", "locale"=>"en"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["remember_token", "5d0fec3c9ccb5afcd28845dd83eb18869f66b4b0"]]
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.4ms)
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
activemodel (4.2.6) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
activerecord (4.2.6) lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
activerecord (4.2.6) lib/active_record/core.rb:566:in `init_attributes'
activerecord (4.2.6) lib/active_record/core.rb:281:in `initialize'
activerecord (4.2.6) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.6) lib/active_record/inheritance.rb:61:in `new'
cancancan (1.14.0) lib/cancan/controller_resource.rb:80:in `build_resource'
cancancan (1.14.0) lib/cancan/controller_resource.rb:61:in `load_resource_instance'
cancancan (1.14.0) lib/cancan/controller_resource.rb:32:in `load_resource'
cancancan (1.14.0) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancancan (1.14.0) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (4.2.6) lib/active_support/callbacks.rb:448:in `instance_exec'
activesupport (4.2.6) lib/active_support/callbacks.rb:448:in `block in make_lambda'
activesupport (4.2.6) lib/active_support/callbacks.rb:164:in `block in halting'
activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `block in call'
activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `each'
activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
bullet (5.1.0) lib/bullet/rack.rb:10:in `call'
clearance (1.14.1) lib/clearance/rack_session.rb:23:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (3.1.1) lib/web_console/middleware.rb:131:in `call_app'
web-console (3.1.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.1.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.1.1) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
puma (3.4.0) lib/puma/configuration.rb:224:in `call'
puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
puma (3.4.0) lib/puma/server.rb:406:in `process_client'
puma (3.4.0) lib/puma/server.rb:271:in `block in run'
puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'
Appears that 'cancancan' gem was interfering with the strong parameters causing the issues with the nested_forms. Since removing the gem it has resolved my issue.

NoMethodError Exception: undefined method `order_item' for #<Order:0x00000009dd52a8>

I have three models, Order, order_item, and food_item
An order has_many order_items and an order_item has_many food_items
When I create a new order from my order_controller API I am getting the below error which am not sure whats wrong.
NoMethodError Exception: undefined method `order_item' for #<Order:0x00000009dd52a8>
orders_controller.rb
class Api::V1::OrdersController < Api::V1::ApplicationController
protect_from_forgery with: :null_session
skip_before_filter :verify_authenticity_token, :if => Proc.new {|c| c.request.format.json? }
before_action :fetch_restaurent
before_action :fetch_food_items, :only => [:index]
after_action :send_order_to_kitchen, :only => [:create, :update]
def index
#orders = Restaurant.orders.fetch_items_from_ethor
end
def show
# Display the list of food_items available from the restaurant
# Allow the customer to place an order
end
def create
#order = Order.new(order_params)
if #order.save
render :json, order, status:201, location: [:api, current_customer, order]
else
render :json, { errors: order.errors }, status:422
end
end
# once the order is placed just send it to the kitechn()
def update
#order = Order.find_by(params[:id])
#rder.update_attributes(order_params)
if #order.save
render :json, order, status: 201, location: [:api, current_user.order.id]
else
render :json,{errors: order.errors}, status: 422
end
end
private
def order_params
params.require(:order).permit(:customer_id, :order_id, :pos_id, :table_id, :order_number,
:order_status,:order_date, :food_item_ids)
end
end
order.rb # model file
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :restaurant
belongs_to :table
has_many :order_items
has_many :food_items, through: :order_items
validates :customer_id, :order_number, presence:true
validates :subtotal, :final_total, presence:true, numericality:{ greater_than_or_equal_to:0 }
end
order_item.rb # model
class OrderItem < ActiveRecord::Base
belongs_to :food_item, inverse_of: :order_items
belongs_to :order, inverse_of: :order_items
belongs_to :restaurant
has_many :food_items
end
food_item.rb # model
class FoodItem < ActiveRecord::Base
belongs_to :restaurant
belongs_to :order, inverse_of: :food_items
belongs_to :order_item, inverse_of: :food_items
has_many :categories_food_items
has_many :categories, through: :categories_food_items
end
I do not understand from where it is asking for order_item. Please help me to understand fix this problem.
undefined method `order_item' for #<Order:0x00000009dd52a8>
My stack trace:
Started POST "/api/v1/orders?access_token=testing&order[customer_id]=1&order[order_number]=101&order[pos_id]=222&order[table_id]=1&order[order_status]=pending&order[order_date]=02/03/2015" for 127.0.0.1 at 2015-06-22 14:39:30 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Api::V1::OrdersController#create as JSON
Parameters: {"access_token"=>"testing", "order"=>{"customer_id"=>"1", "order_number"=>"101", "pos_id"=>"222", "table_id"=>"1", "order_status"=>"pending", "order_date"=>"02/03/2015"}}
ApiKey Load (0.1ms) SELECT `api_keys`.* FROM `api_keys` WHERE `api_keys`.`access_token` = 'testing' LIMIT 1
Restaurant Load (0.1ms) SELECT `restaurants`.* FROM `restaurants` WHERE `restaurants`.`id` IS NULL LIMIT 1
Completed 500 Internal Server Error in 35ms
** [Airbrake] Notice was not sent due to configuration:
Environment Monitored? false
API key set? true
NoMethodError - undefined method `order_items' for #<Class:0x0000000a70f0f8>:
activerecord (4.1.8) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
app/controllers/api/v1/orders_controller.rb:24:in `create'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
searchkick (0.8.7) lib/searchkick/logging.rb:107:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
xray-rails (0.1.16) lib/xray/middleware.rb:37:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.3) lib/rack/etag.rb:23:in `call'
rack (1.5.3) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.3) lib/rack/head.rb:11:in `call'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.3) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.3) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
airbrake (4.2.0) lib/airbrake/rails/middleware.rb:13:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.3) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.3) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.3) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.3) lib/rack/sendfile.rb:112:in `call'
airbrake (4.2.0) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (4.2.0) lib/airbrake/user_informer.rb:12:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.3) lib/rack/content_length.rb:14:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.5) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.5.3) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.3) lib/rack/server.rb:264:in `start'
railties (4.1.8) lib/rails/commands/server.rb:69:in `start'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:81:in `block in server'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:76:in `server'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
railties (4.1.8) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `<main>'
I figured out the reason why it is saying undefined methodorder_item' for #`
I am actually validating the order_item inside the order model. Which am not supposed to.
After changing my order model as below(removing validation on order_item) the problem got solved.
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :restaurant
belongs_to :table
has_many :order_items
has_many :food_items, through: :order_items
validates :customer_id, :order_number, presence:true
validates :subtotal, :final_total, presence:true, numericality:{ greater_than_or_equal_to:0 }

Resources