airblade/paper_trail — Issue #118: info_for_paper_trail can't seem to access params hash using RSpec
Author: spilth
Created: 2012-01-03 22:08:45+00:00
State: open

I'm trying to add the ability for users to provide comments for changes they make. I've taken the following steps:

I created a migration to add a "comments" field to the Versions table and migrated my database:

class AddCommentToVersions < ActiveRecord::Migration
def change
add_column :versions, :comment, :text
end
end

I added `attr_accessible` to `config/initializers/paper_trail.rb`:

class Version < ActiveRecord::Base
attr_accessible :comment
end

In my `app/controllers/application_controller.rb` I create an `info_for_paper_trail` method:

private

def info_for_paper_trail
{ :comment => params[:comment] }
end

I then created a spec to test this out:

describe ComputersController do
context "logged in user" do
before do
@user = Factory(:user)
sign_in @user
end

it "provides 'comments' parameter as version comment" do
computer = Factory(:computer)

put :update, :id => computer.id, :computer_name => "FOOBAR", :comment => "Foobarred"
computer.should have(1).versions
computer.versions[0].comment.should == "Foobarred"
end
end
end

The error I get when running my specs is:

Failure/Error: computer.versions[0].comment.should == "Foobarred"
expected: "Foobarred"
got: nil (using ==)

If I hard code the value of comment in `info_for_paper_trail`, it works correcty. (eg. { :comment => "Foobarred" })

If I `puts params[:comment]` in `info_for_paper_trail` I see it's set to "Foobarred", but for some reason the info_for_paper_trail hash assignment still fails.

Am I missing something?