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 }
Related
I'm versioning my API with Versionist, so right now my routes file looks like
Rails.application.routes.draw do
api_version(:module => "V1", :path => {:value => "v1"}) do
resources :concepts, except: [:new, :edit]
end
end
And my controller is at app/controllers/v1/concepts_controller.rb.
And inside spec/controllers/v1/concepts_controller_spec.rb I have
...
describe "POST #create" do
context "with valid params" do
it "creates a new Concept" do
expect {
post :create, {:concept => valid_attributes}, valid_session
}.to change(Concept, :count).by(1)
end
...
end
end
...
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { {description: 'Sample description.'} }
it "updates the requested concept" do
concept = Concept.create! valid_attributes
put :update, {:id => concept.to_param, :concept => new_attributes}, valid_session
concept.reload
expect(concept.description).to eq('Sample description.')
end
...
The PUT #update tests work fine, but all the POST #create ones give me
1) V1::ConceptsController POST #create with valid params creates a new Concept
Failure/Error: post :create, {:concept => valid_attributes}, valid_session
NoMethodError:
undefined method `concept_url' for #<V1::ConceptsController:0x007fd6e6d8ed38>
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:268:in `handle_model_call'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_dispatch/routing/url_for.rb:167:in `url_for'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:95:in `_process_options'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:43:in `block in _render_to_body_with_renderer'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/rendering.rb:25:in `render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:16:in `render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /usr/local/bundle/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:43:in `render'
# ./app/controllers/v1/concepts_controller.rb:24:in `create'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:198:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `block in instrument'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `instrument'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /usr/local/bundle/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:137:in `process'
# /usr/local/bundle/gems/actionview-4.2.2/lib/action_view/rendering.rb:30:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:632:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:65:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/v1/concepts_controller_spec.rb:38:in `block (5 levels) in <top (required)>'
# ./spec/controllers/v1/concepts_controller_spec.rb:37:in `block (4 levels) in <top (required)>'
My rake routes:
Prefix Verb URI Pattern Controller#Action
v1_concepts GET /v1/concepts(.:format) v1/concepts#index
POST /v1/concepts(.:format) v1/concepts#create
v1_concept GET /v1/concepts/:id(.:format) v1/concepts#show
PATCH /v1/concepts/:id(.:format) v1/concepts#update
PUT /v1/concepts/:id(.:format) v1/concepts#update
DELETE /v1/concepts/:id(.:format) v1/concepts#destroy
How do I get it to use v1_concept_url instead? And why is it even calling concept_url when it isn't even a GET request?'
EDIT My controller:
class V1::ConceptsController < V1::BaseController
before_action :set_concept, only: [:show, :update, :destroy]
# GET /concepts
# GET /concepts.json
def index
#concepts = Concept.all
render json: #concepts
end
# GET /concepts/1
# GET /concepts/1.json
def show
render json: #concept
end
# POST /concepts
# POST /concepts.json
def create
#concept = Concept.new(concept_params)
if #concept.save
render json: #concept, status: :created, location: #concept
else
render json: #concept.errors, status: :unprocessable_entity
end
end
...
Apparently the location header is the culprit. Passing in the instance variable #concept causes Rails to invoke the concept_url helper.
You can override this by invoking the helper yourself:
location: v1_concept_url(#concept)
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?
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.
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.
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