0

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding attributes_for in rspec solved the problem. What a dumb mistake. Thanks!
1

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.

1 Comment

Adding harsh :comm_log => parmas[:comm_log] in controller eliminates the error (NoMethodError...). However the rspec code did not pass. It returned 200 instead of redirect_to a page. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.