Reputation: 10350
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.
Upvotes: 0
Views: 745
Reputation: 84114
This line is the culprit:
get 'create', :customer_id => customer.id, :comm_log => log
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_for
method useful.
As an aside, being able to 'get' a create action is weird.
Upvotes: 2
Reputation: 7015
in the create
method, the new action
expects to get an attributes hash and to stringify it's keys for the column names.
Replace
@comm_log = @customer.comm_log.new(params[:comm_log], :as => :roles_new)
with
@comm_log = @customer.comm_log.new(:comm_log => params[:comm_log], :as => :roles_new)
or as it fits to your column name for the respective model.
Upvotes: 1