Spree - access to Order class - ruby-on-rails

I have a problem with extending a spree controller. When I override the controller I cannot get access to the Order class. I get the following error (I use spree 1.2.4):
NameError (uninitialized constant Spree::Admin::OrdersController::Order):
spree_russian_post/app/controllers/admin/orders_controller_decorator.rb:9:in `sample_method'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
...
The problem is in my controller:
spree_russian_post/app/controllers/admi /orders_controller_decorator.rb
Here is the Code:
require 'spree_core'
Spree::Admin::OrdersController.class_eval do
def sample_method
#order = Order.find_by_param(params[:id]) #Here I get the error
....
end
end
What should I do to get access to the Order class?

Your problem is that Spree thinks you are looking for the constant Spree::Admin::OrdersController::Order
Change your query to #order = Spree::Order.find(params[:id])
Spree will then know that you are looking for the spree object that is associated with the spree_orders table

Related

Please override _params with your desired parameter security

Im developing simple extension for RefineryCMS.
I've already have it in my admin panel.
I have problem with saving. Logs tell me
RuntimeError (Please override block_params with your desired parameter security.):
(eval):157:in `block_params'
(eval):15: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'
I cant find what I need to overrride.
Thank you for advices!
Try doing a bundle update:
bundle update
Got that from this discussion.
Also, check if you have block_params defined in blocks/admin/blocks_controller.rb file. If not add:
def block_params
params.require(:block).permit(. . . )
end

Redis -- undefined method `SMEMBERS' for nil:NilClass

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!

Scope resolution error in rails

module Spree
module Admin
class Spree::Admin::DefaultRedemptionBooksController < Spree::Admin::BaseController
before_filter :authorize_admin
def index
#default_redemption_books = DefaultRedemptionBook.order('year desc, month')
respond_to do |format|
format.html
format.csv { render text: #default_redemption_books.to_csv }
end
end
end
end
when i am running it gives us
NameError (uninitialized constant Spree::DefaultRedemptionBook):
activesupport (3.2.13) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.13) lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
/mnt/processing_dir/librify/librify-apps/shared/bundle/ruby/1.9.1/bundler/gems/spree_auth_devise-a6ae3646234d/app/controllers/spree/admin/admin_controller_decorator.rb:8:in `model_class'
/mnt/processing_dir/librify/librify-apps/shared/bundle/ruby/1.9.1/gems/spree_backend-2.0.3/app/controllers/spree/admin/base_controller.rb:19:in `authorize_admin'
activesupport (3.2.13) lib/active_support/callbacks.rb:506:in `_run__2536621662702313430__process_action__1018742768700982955__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal
I
have already DefaultRedemptionBook but it trying to find in spree please help me
My problem was solved
Renamedd DefaultRedemptionBooksController and its working fine because I have two classes as same name so it confused and controller with call another file

Error getting setting of my custom plugin for Redmine

Hy, everyone!
I have an error in ruby code of my plugin for Redmine. Error occurs then method Setting.plugin_myplugin['myplugin_setting']
is called.
Here traceback of this error:
NoMethodError (undefined method `plugin_myplugin' for #<Class:0x00000004874098>):
activerecord (3.2.17) lib/active_record/dynamic_matchers.rb:55:in `method_missing'
plugins/myplugin/lib/myplugin/patches/account_controller_patch.rb:36:in `login_with_myplugin'
actionpack (3.2.17) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.17) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.17) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.17) lib/active_support/callbacks.rb:447:in `_run__4412462782733274046__process_action__1678150651014813342__callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.17) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.17) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.17) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.17) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.17) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.17) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.17) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.17) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.17) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.17) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.17) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.17) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
System specifications:
Redmine 2.5.1
apache2.2.22-1ubuntu1.5
Passenger 4.0.4
ruby 1.9.3p545
Does anyone know what could be the problem?
It is necessary to debug code.
Here Redmine loads plugin settings to class variable.
Here Redmine defines setter/geter for each setting.
And exactly your method plugin_myplugin does not appear in ##available_settings. So I think you should
check your init.rb file and check what plugin name you use.
check if any settings you define there (working example)
(if previous didn't help) debug core: check why your plugin settings don't appear in ##avaulable_settings (I think you should debug here)
This problem happened also to me.
With #gotva's answer suggestions I've been able to go deeper and I understood which was the cause for me. In my case, I've got 2 plugins:
one plugin has got its settings (registered in the standard way, i.e. calling the method settings inside Redmine::Plugin.register), and I was failing to reach its configuration page seeing the error reported in the question;
in the other plugin I was setting a given Setting (sorry for the pun) just after having registered the plugin (i.e. after the call to register).
The plugin with settings was working if I was keeping only it.
Since the Setting class loads settings when it's used for the first time, I'm pretty sure that the problem was that the plugin in which I was using Setting was getting loaded before the other: so when I declared the settings, Setting didn't load them anymore because it was been already called, leaving out the settings that I wanted to load.

Acronym won't pluralize correctly causing circular dependencies

I would like to name my controller ESCsController, ESC being the acronym in question. I found the rails inflection docs which describe a way in which to accomplish this.
http://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html
Note: Acronyms that are passed to pluralize will no longer be recognized, since the acronym will not occur as a delimited unit in the pluralized result. To work around this, you must specify the pluralized form as an acronym as well:
acronym 'API'
camelize(pluralize('api')) #=> 'Apis'
acronym 'APIs'
camelize(pluralize('api')) #=> 'APIs'
I created my controller and models after adding this to environment.rb
ActiveSupport::Inflector.inflections { |i|
i.acronym 'ESC'
i.acronym 'ESCs'
}
Tested in the console these work perfectly. 'ESC'.pluralize() returns ESCs and 'ESCs'.singularize() returns ESC as expected
Controllers and models were generated through rails generate model ESC and rails generate controller ESCs respectively. This created escs_controller.rb and the model esc.rb as expected.
In my routes.rb I used to have
resources :ESCs, path: '/parts/escs'
which returned this error: 'ESCs' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
I ended up changing it to:
resources :ESCs, controller: 'escs', path: '/parts/escs'
However, now whenever I try and visit a page I get a circular dependency error:
Circular dependency detected while autoloading constant EscsController
Anyone know what's going on? It looks like whatever is trying to load the controller isn't seeing that it should be ESCsController and not EscsController. I'm new to rails so this could be a simple problem...
I'm using Rails 4.0.2
Here's the relevant part of the full stack trace.
activesupport (4.0.2) lib/active_support/dependencies.rb:461:in `load_missing_constant'
activesupport (4.0.2) lib/active_support/dependencies.rb:184:in `const_missing'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:226:in `const_get'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:226:in `block in constantize'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `each'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `inject'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `constantize'
activesupport (4.0.2) lib/active_support/dependencies.rb:535:in `get'
activesupport (4.0.2) lib/active_support/dependencies.rb:566:in `constantize'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:76:in `controller_reference'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:66:in `controller'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:44:in `call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'
Try moving code that adds the new inflections from environment.rb to config/initializers/inflections.rb then reload the server. Also, you don't need to specify a controller option for your route, just make it resources :escs, path: '/parts/escs' and it shall work fine.

Resources