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

  • SEARCH
  • Home
  • 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 7018389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:02:40+00:00 2026-05-27T23:02:40+00:00

I am using acts_as_audited. In the controller on the destroy action I pass the

  • 0

I am using acts_as_audited. In the controller on the destroy action I pass the audit comment value. All this works well but when I try to test the destroy action I get:

PurchasesController DELETE /destroy deletes the correct Purchase
 Failure/Error: delete :destroy, id: i
 NoMethodError:
  You have a nil object when you didn't expect it!  
  You might have expected an instance of Array.  
  The error occured while evaluating nil.[] 
# ./app/controllers/purchases_controller.rb:79:in `destroy'
# ./spec/controllers/purchases_controller_spec.rb:156:in `block (3 levels) in <top (required)>'

Line #79 reads: @purchase.audit_comment = params[:purchase][:audit_comment]

Heres my code:

PurchasesController

def destroy
  @purchase = Purchase.find(params[:id])
  @purchase.audit_comment = params[:purchase][:audit_comment]
  respond_to do |format|
    if @purchase.destroy
      format.html { redirect_to(purchases_url, notice: "Successfully destroyed purchase.") }
      format.xml  { render :xml => @purchase, :status => :deleted, :location => @purchase }
    else
      flash[:alert] = "#{@purchase.po_number} could not be destroyed"
      render 'show'
    end
  end
end

Purchases show.html.erb EDITED

<%= form_for @purchase, method: :delete do |builder| %>

<% if @purchase.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@purchase.errors.count, "error") %> prohibited this purchase from being saved:</h2>

    <ul>
    <% @purchase.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<% title "Purchase" %>

<div id="delete_button">    
  <span><strong>PO Number:  <%= @purchase.po_number %></strong></span>
    <%= render "audit_comment", f: builder %>
    <%= builder.submit "Destroy Purchase"%>
</div>  
<% end %>
<p>
  <%= link_to "Edit", edit_purchase_path(@purchase) %> |
  <%= link_to "View All", purchases_path %>
</p>

_audit_comment.html.erb – Purchases

<div id = "audit">
  <%= f.label :audit_comment %>:<br />
  <%= f.text_area :audit_comment, :size => "60x5" %>
</div>

purchase_controller_spec.rb

require 'spec_helper'
require 'ruby-debug'

describe PurchasesController do
  login_user
  render_views

  before(:each) do
    @purchase = Factory(:purchase)
  end

  describe "DELETE /destroy" do
    before(:each) do
      @ability.can :destroy, Purchase      
    end  
    it "deletes the correct Purchase" do
      i = @purchase.id
      c = Purchase.count
      pl = Purchase.find(i).purchase_line_items
      cpl = pl.count
      delete :destroy, id: i
      Purchase.count.should == c-1
      pl.count.should == cpl-1
      response.should redirect_to(purchases_path)
      flash[:notice].should == "Successfully destroyed purchase."
    end
    it "redirects to the index page with an alert when a delete fails" do
      i = @purchase.id
      c = Purchase.count
      pl = Purchase.find(i).purchase_line_items
      cpl = pl.count
      Purchase.any_instance.stubs(:valid?).returns(:false)
      delete :destroy, id: i
      Purchase.count.should_not == c-1
      pl.count.should_not == cpl-1
      response.should render_template('show')
      flash[:alert].should == "#{@purchase.po_number} could not be destroyed"
    end
  end
end

Any help is appreciated. Thanks!

  • 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-27T23:02:41+00:00Added an answer on May 27, 2026 at 11:02 pm

    It’s telling you that at line 79, params[:purchase] is nil.

    The reason it’s nil is that button_to generates its own form tag. Thus, you now have a <form> within a <form>, and your audit comment field is not being submitted. Instead of button_to, you should use builder.submit. You will also need to set the :method option in your call to form_for to make it a DELETE request.

    Update after edit to question

    The HTML looks OK now, but I see a problem with the spec. I think you’re forgetting to pass the audit comment in to your HTTP params. It’s there in the HTML, but your spec bypasses the form, because it tests the controller in isolation. (An integration test would use the actual form. Controller tests don’t.) Therefore, you’ll have to manually add to the request any form params the controller expects. For example:

    delete :destroy, :id => 1, :purchase => {:audit_comment => 'Test comment'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I want to use Devise and acts_as_audited together but when I try and
Using the acts_as_taggable_on works great. But generates a comma separated list. I'm wondering the
My controller action is calling all images belonging to a specific user and Im
I'm using acts_as_taggable_on and I'm trying to query for all users tagged with any
I'm using the acts_as_auditable plugin, and the revisions attribute gives me this chunk of
I am using acts_as_commentable. This is how the Model has comments added to it:
I am using acts_as_ferret(0.4.3) to do full-text searching, but when update index I need
I'm trying to install acts_as_ferret in windows7.But am getting an error Using acts-as-taggable-on (2.1.1)
Using: Rails 3.1.1 I have a controller/model/view called Category that is built up on
I'm using acts-as-taggable-on. I've tried multiple approaches but none of them work. On the

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.