Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1039903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:09:28+00:00 2026-05-16T15:09:28+00:00

Please bear with me as I’m a bit new to rails (especially rails 3),

  • 0

Please bear with me as I’m a bit new to rails (especially rails 3), and I’m kinda stumped by this. Basically I want to test my CustomersController in my application using RSpec. When I execute my specs trying to perform a post to the create action in my controller, I get this error, and I’m not sure why:

1) CustomersController as guest should render new template when executing create action with invalid model
    Failure/Error: post :create, :customer => model
    protected method `reject' called for #<Customer:0x0000010611c400>
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.beta.16/lib/mongoid/attributes.rb:23:in `method_missing'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0.rc/lib/active_model/mass_assignment_security/sanitizer.rb:6:in `sanitize'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0.rc/lib/active_model/mass_assignment_security.rb:153:in `sanitize_for_mass_assignment'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.beta.16/lib/mongoid/attributes.rb:42:in `process'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.beta.16/lib/mongoid/document.rb:104:in `initialize'
    # ./app/controllers/customers_controller.rb:11:in `new'
    # ./app/controllers/customers_controller.rb:11:in `create'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/base.rb:136:in `process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/rendering.rb:11:in `process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:429:in `_run__4199577925444808436__process_action__3619821760006806352__callbacks'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:404:in `_run_process_action_callbacks'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:93:in `run_callbacks'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/callbacks.rb:17:in `process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/notifications.rb:52:in `block in instrument'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/notifications.rb:52:in `instrument'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/rescue.rb:17:in `process_action'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/base.rb:105:in `process'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/rendering.rb:40:in `process'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/test_case.rb:404:in `process'
    # /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/test_case.rb:347:in `post'
    # ./spec/controllers/customers_controller_spec.rb:14:in `block (2 levels) in <top (required)>'

Now I’m really sorry, but here’s a a LOT of code to follow. First is my gems.

mongoid (2.0.0.beta.16)
mongoid-rspec (1.2.0)
...
rspec (2.0.0.beta.19)
rspec-core (2.0.0.beta.19)
rspec-expectations (2.0.0.beta.19)
rspec-mocks (2.0.0.beta.19)
rspec-rails (2.0.0.beta.19)
...
rails (3.0.0.rc)
...
recaptcha (0.2.3)

Now, I’ve got my CustomersController defined as follows:

class CustomersController < ApplicationController

  respond_to :html
  before_filter :authenticate_customer!, :except => [ :new, :create ]

  def new
    @customer = Customer.new
  end

  def create
    @customer = Customer.new(params[:customer])
    if verify_recaptcha(:model => @customer, :message => "Image verification failed.  Please try again.") && @customer.save 
      flash[:notice] = "Your account has been created successfully"
    end
    respond_with @customer
  end

  def show
  end
end

Here’s my customer model:

class Customer
  include Mongoid::Document
  devise :database_authenticatable, :recoverable, :rememberable,
    :trackable, :validatable

  field :first_name
  field :last_name
  field :email
  field :roles, :type => Array, :default => [ 'User' ]

  embeds_many :servers

  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :email
  validates_presence_of :roles

  validates_uniqueness_of :email

  attr_accessible :first_name, :last_name, :email,
                  :password, :password_confirmation

  def role?(role)
    return !!self.roles.include?(role.to_s.camelize)
  end

end

Finally, here’s my spec:

describe CustomersController, "as guest" do
  include Devise::TestHelpers

  it "should render new template when executing new action" do
    get :new
    response.should render_template(:new)
  end

  it "should render new template when executing create action with invalid model" do
    model = Customer.new(:first_name => "Test", :last_name => "user")
    post :create, :customer => model
    response.should render_template(:new)
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:09:29+00:00Added an answer on May 16, 2026 at 3:09 pm

    Well it turns out that I had to pass a tuple instead of the actual model object in each of my tests. I switched to using factory_girl to create my model objects, and then called the attributes_for method on each model object. I passed that result to the create action, and everything worked as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm new to rails so please bear with me :) I want to
I am new to bash so please bear with me if this is a
This is kinda confusing so please bear with me. I have an array (listOfStates)
I'm a fairly new programmer so please bear with me on this. I am
Please bear with me, I am new to Uniscribe so I hope this isn't
I'm new to Lua so please bear with this simple question :) I'm simply
I'm new to this so please bear with me. I'm dealing with the Twitter
I'm pretty new to this so please bear with me. I'm trying to make
I am relatively new to Delphi so please bear with me. Basically, I need
Bit of a Rails newbie here so please bear with me. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.