Here is a comm_logs controller ‘create’
def create
if has_create_right?
@customer = Customer.find(params[:customer_id])
@comm_log = @customer.comm_log.new(params[:comm_log], :as => :roles_new)
@comm_log.input_by_id = session[:user_id]
if @comm_log.save
#send out email
redirect_to URI.escape("/view_handler?index=0&msg=Log saved!")
else
flash.now[:error] = "can't save log!"
render 'new'
end
else
redirect_to URI.escape("/view_handler?index=0&msg=insufficient rights!")
end
end
The rspec code is:
describe "'create'" do
it "should be successful for sales member" do
session[:sales] = true
session[:user_id] = 1
session[:member] = true
customer = Factory(:customer)
log = Factory(:comm_log, :customer_id => customer.id)
get 'create', :customer_id => customer.id, :comm_log => log
response.should redirect_to URI.escape("/view_handler?index=0&msg=Log saved!")
end
end
Here is the rspec error:
1) CommLogsController 'create' should be successful for sales member
Failure/Error: get 'create', :customer_id => customer.id, :comm_log => log
NoMethodError:
undefined method `stringify_keys' for "1":String
# c:in `build'
# ./app/controllers/comm_logs_controller.rb:30:in `create'
# ./spec/controllers/comm_logs_controller_spec.rb:79:in `block (3 levels) in <top (required)>'
The exact same code (controller and rspec) worked for another similar controller.
Any thoughts about the problem? Thanks.
This line is the culprit:
You’re trying to stuff an actual activerecord instance into the params hash. Rails knows that parameters can’t be objects like that so it sends the id instead. You then try and use that id as if it was a hash, which obviously doesn’t work.
Instead pass what would actually be submitted: a hash of values to create the object with. You may find factory girl’s
attributes_formethod useful.As an aside, being able to ‘get’ a create action is weird.