Rails + bunny gem - no such file to load -- bunny - ruby-on-rails

I have a rails controller:
class Console::AccesorBillsController < ConsoleController
...
def bill
AcceccorBill.new.run
render :nothing => true
end
end
I create new model there and run a method with an example from bunny's page (http://rubybunny.info/articles/getting_started.html):
require "bunny"
class AcceccorBill
def initialize()
end
def run
conn = Bunny.new
conn.start
ch = conn.create_channel
x = ch.fanout("nba.scores")
ch.queue("joe", :auto_delete => true).bind(x).subscribe do |delivery_info, metadata, payload|
puts "#{payload} => joe"
end
ch.queue("aaron", :auto_delete => true).bind(x).subscribe do |delivery_info, metadata, payload|
puts "#{payload} => aaron"
end
ch.queue("bob", :auto_delete => true).bind(x).subscribe do |delivery_info, metadata, payload|
puts "#{payload} => bob"
end
x.publish("BOS 101, NYK 89").publish("ORL 85, ALT 88")
conn.close
end
end
But i always get this error:
NameError - uninitialized constant AcceccorBill::Bunny:
(gem) activesupport-3.1.12/lib/active_support/dependencies.rb:507:in `load_missing_constant'
(gem) activesupport-3.1.12/lib/active_support/dependencies.rb:181:in `block in const_missing'
(gem) activesupport-3.1.12/lib/active_support/dependencies.rb:179:in `const_missing'
(gem) rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing'
app/ext/acceccor_bill.rb:6:in `initialize'
app/controllers/console/accesor_bills_controller.rb:17:in `bill'
(gem) actionpack-3.1.12/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
(gem) actionpack-3.1.12/lib/abstract_controller/base.rb:167:in `process_action'
(gem) actionpack-3.1.12/lib/action_controller/metal/rendering.rb:10:in `process_action'
Does anyone know how to solve that?
P.S.: I installed gem via bundler and it looks like require doesn't work.

Solved it.
I forgot to require the bundler. So i just added this in the begining of file:
require "bundler/setup"
require "bunny"
And that is all.

Related

Rails Integration testing with rspec and fixture_file_upload

I recently ran into an issue with an integration test using rspec.
RSpec.describe "Images", type: :request do
before(:each) do
#user = User.create(username: "sam", password: "password")
#tag = #user.tags.create(name: "outdoors")
post login_path, params: { username: "sam", password: "password" }
expect(session[:user_id]).to eql #user.id
#file = fixture_file_upload("/dingo.jpeg", "image/jpeg")
end
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
expect(response).to be_success
end
end
The before each creates a valid user, tag and logs the user in. Then it loads an image file.
The test it's self creates a new image for the #user and assigns it a value for caption and the loaded file for its :image_file.
The Image model uses has_one_attached :image_file to associate an uploaded image with an image record
class Image < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :tags
has_one_attached :image_file
end
the controller and action as follows:
class ImageController < ApplicationController
before_action :unauthorized_redirect
def show
#user = current_user
#image = Image.find(params[:id])
#tags = #user.tags.all
authorize #image, :show?
end
private
def image_params
params.require(:image).permit(:caption, :image_file, tag_ids: [])
end
end
When I run the test I get an error:
Failures:
1) Images can show images
Failure/Error: <%= image_tag #image.image_file, class: "img-fluid show-image" %>
ActionView::Template::Error:
Can't resolve image into URL: undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
# <internal:kernel>:90:in `tap'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/rendering.rb:20:in `with_renderer'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/tempfile_reaper.rb:15:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/etag.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/conditional_get.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:266:in `context'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:37:in `call_app'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `block in call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/engine.rb:539:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/integration.rb:16:in `block (2 levels) in <module:Integration>'
# ./spec/requests/images_spec.rb:38:in `block (2 levels) in <main>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
Finished in 0.79372 seconds (files took 0.46552 seconds to load)
30 examples, 1 failure
Failed examples:
rspec ./spec/requests/images_spec.rb:36 # Images can show images
I have debugged this issue with breakpoints everywhere and I just don't understand why it's not working in test when it works in production. I hope I've provided enough information this bug has been driving me crazy for the past two days.
Thank you!
Versions:
rails: 6.1.4.1
rspec: 5.0.2
Changed one line and fixed it
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
*expect(response).to be_success ----REMOVE*
expect(response.successful?).to eql true
end

RSPEC test fails to POST a "create" action for nested resource

I have a resource called "parameters" that is nested under "tests" in my API. There relation is as follows: "A test has many parameters". I've structured my routes as follows
tests/:test_id/parameters/:parameter_id
and my rake routes gives:
test_parameters GET /tests/:test_id/parameters(.:format) parameters#index {:format=>:json}
POST /tests/:test_id/parameters(.:format) parameters#create {:format=>:json}
new_test_parameter GET /tests/:test_id/parameters/new(.:format) parameters#new {:format=>:json}
edit_test_parameter GET /tests/:test_id/parameters/:id/edit(.:format) parameters#edit {:format=>:json}
test_parameter GET /tests/:test_id/parameters/:id(.:format) parameters#show {:format=>:json}
PATCH /tests/:test_id/parameters/:id(.:format) parameters#update {:format=>:json}
PUT /tests/:test_id/parameters/:id(.:format) parameters#update {:format=>:json}
DELETE /tests/:test_id/parameters/:id(.:format) parameters#destroy {:format=>:json}
I'm writing simple rspec tests to test all the typical actions of my API. Right now, my POST/CREATE is giving me a very puzzling error.
Failures:
1) ParametersController POST create a new parameter with valid attributes successfully with name and times
Failure/Error: post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
NoMethodError:
undefined method `parameter_url' for #<ParametersController:0x000001072b4280>
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:114:in `polymorphic_url'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/url_for.rb:163:in `url_for'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:95:in `_process_options'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/streaming.rb:200:in `_process_options'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:43:in `block in _render_to_body_with_renderer'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/rendering.rb:25:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:16:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:43:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:258:in `display'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:214:in `api_behavior'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:191:in `rescue in to_format'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:185:in `to_format'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:163:in `respond'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:156:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/respond_with.rb:203:in `respond_with'
# ./app/controllers/parameters_controller.rb:13:in `create'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:137:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionview-4.2.2/lib/action_view/rendering.rb:30:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:632:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:65:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/parameters_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
# ./spec/controllers/parameters_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
I've tried changing the formats in how I'm submitting the POST but that hadn't changed anything. For now, my rspec test looks like this:
"spec/controllers/parameters_controller_spec.rb"
require 'spec_helper'
describe ParametersController do
before :each do
#test = FactoryGirl.create(:test, id: 1, name: 'Eccentric Tech')
#test.save
end
# ...
describe 'POST create a new parameter' do
context 'with valid attributes' do
it 'successfully with name and times' do
parameter = FactoryGirl.build(:parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(1)
end
end
context 'with invalid attributes' do
it 'fails without a name' do
parameter = FactoryGirl.build(:no_name_parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(0)
end
it 'fails without belonging to a test' do
parameter = FactoryGirl.build(:no_test_parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(0)
end
end
end
# ...
end
My Factory:
"spec/factories/parameters.rb"
require 'faker'
FactoryGirl.define do
factory :parameter do |f|
f.name { "#{Faker::Hacker.noun}" }
f.value { Faker::Number.number(2) }
f.test_id { 1 }
end
factory :no_name_parameter, parent: :parameter do |f|
f.name nil
f.value { Faker::Number.number(2) }
f.test_id { 1 }
end
factory :no_test_parameter, parent: :parameter do |f|
f.name { "#{Faker::Hacker.noun}" }
f.value { Faker::Number.number(2) }
f.test_id nil
end
end
And finally, my controller:
class ParametersController < ApplicationController
respond_to :json
def index
respond_with Parameter.all
end
def show
respond_with Parameter.find(params[:id])
end
def create
respond_with Parameter.create(parameter_params)
end
def update
respond_with Parameter.update(params[:id], parameter_params)
end
def destroy
respond_with Parameter.destroy(params[:id])
end
private
def parameter_params
params.require(:parameter).permit(:name, :value, :test_id)
end
end
routes.rb:
Rails.application.routes.draw do
scope :defaults => { :format => :json } do
resources :services do
resources :tests
end
resources :tests do
resources :parameters
resources :singular_results
resources :dataset_results
end
end
end
I solved this myself but I have nooooo idea why it works. If someone could explain it please do :)
All I did was make this small change to my controller:
def create
respond_with Parameter.create(parameter_params), location: nil
end
Found a good explanation: Rails: NoMethodError (Undefined method _url for _controller. I can't seem to respond_with json properly after my create. Why?

Tests for Devise user that has polymorphic profiles

My application has an User model that belongs_to :profile, :polymorphic => true. This profile can be Student, Manager, Enterprise or Employee and it needs to be approved, by its administrator, to log on the system. I used this approval approach.
I'm having a little troubles with the test part. It always outputs:
1) Error:
test_should_get_dashboard(ManagersControllerTest):
NoMethodError: undefined method `approved' for nil:NilClass
Probably relevant sources:
class User < ActiveRecord::Base
belongs_to :profile, :polymorphic => true
attr_accessible :approved
end
class ManagersControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :managers
fixtures :users
def setup
manager = Manager.create(:name => 'Manager', :cpf => '537.846.919-86')
#user = User.new(:email => 'user#user.com', :password => 'user#user.com')
#user.approved = true
#user.profile = manager
#user.save!
end
test "should get dashboard" do
sign_in #user
get :dashboard
assert_response :redirect
assert_redirected_to managers_root_path
end
end
class ManagersController < ApplicationController
def dashboard
p = Proc.new { |e| [e.name, e.id] if e.user.approved }
app_en = Enterprise.all.map(&p).compact.count
napp_en = Enterprise.count - app_en
app_em = Employee.all.map(&p).compact.count
napp_em = Employee.count - app_em
#app = [ app_en, app_em, 0 ]
#napp = [ napp_en, napp_em, 0 ]
#total = [ app_en + napp_en, app_em + napp_em, Student.count ]
tags_count = Activity.tag_counts.map(&:count)
#tags = Hash[Activity.tag_counts.map(&:name).zip(tags_count)]
#user = current_user
end
end
As I'm having troubles with the fixtures written, I'm ignoring them, but I would be very thankful if someone show a solution with fixtures. Even this way I'm getting stuck in this error. What should I do?
The Stack
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:7:in `block in dashboard'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:8:in `map'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:8:in `dashboard'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:167:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/rendering.rb:10:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:425:in `_run__2212830968385571096__process_action__4077155092621945467__callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:17:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/rescue.rb:29:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.6/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:121:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:45:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:469:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:49:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `block in process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `catch'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `_catch_warden'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:386:in `get'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/test/functional/manager_controller_test.rb:24:in `block in <class:ManagersControllerTest>'
The answer appears to be that not all the Enterprise or Employee models actually have an associated user. When using map to e.user.approved, the controller tries to pass .approved to user which can be nil in this case.
One simple solution is to change your Proc to be nil safe:
p = Proc.new { |e| [e.name, e.id] if e.user && e.user.approved }

Rails: Action error in production but not in development

Can anyone explain why I keep getting an error in production but not in development? Relevant parts:
get: /user/logout
ActionController::RoutingError (uninitialized constant User::SessionController):
activesupport/lib/active_support/inflector/methods.rb:229:in `block in constantize'
activesupport/lib/active_support/inflector/methods.rb:228:in `each'
activesupport/lib/active_support/inflector/methods.rb:228:in `constantize'
actionpack/lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
actionpack/lib/action_dispatch/routing/route_set.rb:54:in `controller'
actionpack/lib/action_dispatch/routing/route_set.rb:32:in `call'
journey/lib/journey/router.rb:68:in `block in call'
journey/lib/journey/router.rb:56:in `each'
journey/lib/journey/router.rb:56:in `call'
actionpack/lib/action_dispatch/routing /route_set.rb:600:in `call'
omniauth/lib/omniauth/strategy.rb:177:in `call!'
omniauth/lib/omniauth/strategy.rb:157:in `call'
omniauth/lib/omniauth/builder.rb:48:in `call'
airbrake/lib/airbrake/rack.rb:27:in `call'
Routes:
Application1::Application.routes.draw do
match('/auth/:provider/callback' => 'session#create', :format => false)
root(:to => 'blog/archives#index', :format => false)
namespace(:user) do
match('/logout' => 'session#destroy', :format => false)
end
namespace(:blog) do
match('/archive/:slug' => 'archive#show', :format => false)
constraints(:page => /page\d+/) do
match('/archives/:page' => 'archives#index', :format => false)
end
end
end
I am using Rails 3.2.3 with the latest Omniauth.
You have created a namespace user and therefore you should place the session controller that defines the destroy action in this path:
/app/controllers/user/session_controller.rb
Then you can do stuff like:
Create a file in /app/controller/user/base_controller.rb that defines this:
class User::BaseController < ApplicationController
# Whatever you want here
end
And define the sessions controller located in /app/controllers/user/session_controller.rb as:
class Users::SessionsController < User::BaseController
def destroy
# whatever you want destroy to do..
end
end
Read this for more documentation about namespaces and routing.

XML Builder in Rails: 'wrong number of arguments' bug that I just can't trace

New to rails, and I've just spent hours trying to hunt down a bug. Any help very appreciated.
I'm trying to make a rails page so that if a user enters /info/who_bought/1 the page outputs HTML, and if they enter /info/who_bought/1.xml, the page returns an XML file. (In short, I'm doing chapter 11 from the 'Agile Web Development with Rails' book)
The HTML-formatted output works fine, but if I enter the xml I consistently get a 'wrong number of arguments (1 for 0)' error page. Downloaded the actual ebook and did a direct copy/paste of all code; still facing the same problem. Google doesn't bring up any mention of code bugs.
Code & full stack trace below. If anyone has any ideas I'd be very appreciative. In short - what could possible cause xml.builder to return a '1 for 0 arguments' bug?
Many thanks,
Baggage
# app/controllers/info_controller.rb
class InfoController < ApplicationController
def who_bought
#product = Product.find(params[:id])
#orders = #product.orders
respond_to do |format|
format.html
format.xml {render :layout => false }
end
end
protected
def authorize
end
end
# app/views/info/who_bought.xml.builder
xml.order_list(:for_product => #product.title) do
for o in #orders
xml.order do
xml.name(o.name)
xml.email(o.email)
end
end
end
# app/models/product.rb
class Product < ActiveRecord::Base
has_many :orders, :through => :line_items
has_many :line_items
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validates_length_of :title,
:minimum => 10
validates_format_of :image_url,
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG ' +
'or PNG image.'
validate :price_must_be_at_least_a_cent
def price_must_be_at_least_a_cent
errors.add(:price, 'should be at least 0.01') if price.nil? || price < 0.01
end
def self.find_products_for_sale
find(:all, :order => "title")
end
end
# config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :users
map.resources :line_items
map.resources :orders
map.resources :products
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
# Full trace
/usr/lib/ruby/1.8/builder/xmlbase.rb:134:in `to_xs'
/usr/lib/ruby/1.8/builder/xmlbase.rb:134:in `_escape'
/usr/lib/ruby/1.8/builder/xmlbase.rb:87:in `text!'
/usr/lib/ruby/1.8/builder/xmlbase.rb:144:in `_newline'
/usr/lib/ruby/1.8/builder/xmlbase.rb:60:in `method_missing'
app/views/info/who_bought.xml.builder:2
vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:359:in `method_missing'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `method_missing'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `each'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `send'
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `method_missing'
vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:359:in `method_missing'
app/views/info/who_bought.xml.builder:1
vendor/rails/actionpack/lib/action_view/renderable.rb:39:in `send'
vendor/rails/actionpack/lib/action_view/renderable.rb:39:in `render'
vendor/rails/actionpack/lib/action_view/template.rb:73:in `render_template'
vendor/rails/actionpack/lib/action_view/base.rb:256:in `render'
vendor/rails/actionpack/lib/action_controller/base.rb:1177:in `render_for_file'
vendor/rails/actionpack/lib/action_controller/base.rb:940:in `render_without_benchmark'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render'
vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:8:in `realtime'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render'
app/controllers/info_controller.rb:6
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:135:in `call'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:135
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:164:in `call'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:164:in `respond'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:158:in `each'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:158:in `respond'
vendor/rails/actionpack/lib/action_controller/mime_responds.rb:107:in `respond_to'
app/controllers/info_controller.rb:5:in `who_bought'
vendor/rails/actionpack/lib/action_controller/base.rb:1256:in `send'
vendor/rails/actionpack/lib/action_controller/base.rb:1256:in `perform_action_without_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
vendor/rails/actionpack/lib/action_controller/rescue.rb:136:in `perform_action_without_caching'
vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache'
vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
vendor/rails/actionpack/lib/action_controller/base.rb:524:in `send'
vendor/rails/actionpack/lib/action_controller/base.rb:524:in `process_without_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:606:in `process_without_session_management_support'
vendor/rails/actionpack/lib/action_controller/session_management.rb:134:in `process'
vendor/rails/actionpack/lib/action_controller/base.rb:392:in `process'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:184:in `handle_request'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:112:in `dispatch_unlocked'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:125:in `dispatch'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:124:in `synchronize'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:124:in `dispatch'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:134:in `dispatch_cgi'
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:41:in `dispatch'
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in `process'
/usr/lib/ruby/1.8/mongrel/rails.rb:74:in `synchronize'
/usr/lib/ruby/1.8/mongrel/rails.rb:74:in `process'
/usr/lib/ruby/1.8/mongrel.rb:159:in `process_client'
/usr/lib/ruby/1.8/mongrel.rb:158:in `each'
/usr/lib/ruby/1.8/mongrel.rb:158:in `process_client'
/usr/lib/ruby/1.8/mongrel.rb:285:in `run'
/usr/lib/ruby/1.8/mongrel.rb:285:in `initialize'
/usr/lib/ruby/1.8/mongrel.rb:285:in `new'
/usr/lib/ruby/1.8/mongrel.rb:285:in `run'
/usr/lib/ruby/1.8/mongrel.rb:268:in `initialize'
/usr/lib/ruby/1.8/mongrel.rb:268:in `new'
/usr/lib/ruby/1.8/mongrel.rb:268:in `run'
/usr/lib/ruby/1.8/mongrel/configurator.rb:282:in `run'
/usr/lib/ruby/1.8/mongrel/configurator.rb:281:in `each'
/usr/lib/ruby/1.8/mongrel/configurator.rb:281:in `run'
/usr/bin/mongrel_rails:129:in `run'
/usr/lib/ruby/1.8/mongrel/command.rb:212:in `run'
/usr/bin/mongrel_rails:282
This is a conflict between Rails 3.1, Builder 3.0, and either the fast_xs gem or hpricot (which bundles fast_xs). The reason why some people can't reproduce it is that they don't have fast_xs or hpricot in their Gemfile.
Unfortunately, since it's a three-way conflict, everybody thinks it's the other library's fault:
Builder bug report
Rails pull request
fast_xs bug report
In the meantime, pile another monkey patch on the heap and add config/initializers/unbreak_string_to_xs.rb with the following:
class String
def fast_xs_absorb_args(*args); fast_xs; end
alias_method :to_xs, :fast_xs_absorb_args
end
(From this answer to a duplicate.)
Try (re)installing the 'builder' gem, that fixed the '1 for 0 arguments' bug for me :)
sudo gem install builder
Don't forget to restart your server afterwards
I would kind of like to blame the "for o in #orders" part. Perhaps trying with a traditional way would help to eliminate this suspicion:
#orders.each do |o|
...
end

Resources