undefined method `stringify_keys' for #<User:0xc9a70c0> #223

Closed
asishtripathy opened this Issue Oct 5, 2012 · 5 comments

Comments

Projects
None yet
2 participants

I am using clearance for authentication and is working fine too. But when i am writing the specs for controller and creating the session with sign_in method of clearance, I get following error:-

undefined method `stringify_keys' for #<User:0xc9a70c0>

In my controller i have

before_filter :authorize

In controller specs

def valid_session
  sign_in
end

In factories/clearance.rb

FactoryGirl.define do
  sequence :email do |n|
    "user#{n}@example.com"
  end

  factory :user do
    email
    password "password"
    fname "fname"
    lname "lname"
  end
end

Here is how my controller looks like :-

class CompaniesController < ApplicationController

before_filter :authorize
load_and_authorize_resource

def index
  @companies = Company.all
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @companies }
  end 
end

def show
  @company = Company.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @company }
  end 
end 

def new  
  @company = Company.new
  @company_manager = @company.users.build
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @company }
  end 
end 

def edit
  @company = Company.find(params[:id])
end 

def create   
  @company = Company.new(params[:company])
  respond_to do |format|
    if @company.save
      format.html { redirect_to companies_url, notice: 'Company was successfully created.' }
      format.json { render json: @company, status: :created, location: @company }
    else
      format.html { render action: "new" }
      format.json { render json: @company.errors, status: :unprocessable_entity }
    end 
  end 
end 

def update
  @company = Company.find(params[:id])
  respond_to do |format|
    if @company.update_attributes(params[:company])
      format.html { redirect_to companies_url, notice: 'Company was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @company.errors, status: :unprocessable_entity }
    end 
  end 
end 

def destroy
  @company = Company.find(params[:id])
  @company.destroy

  respond_to do |format|
    format.html { redirect_to companies_url }
    format.json { head :no_content }
  end 
end 

end 

Please help me. Thanks in advance.

Contributor

croaky commented Oct 6, 2012

Is there any other code to show, perhaps in the controller?

Usually this error occurs when we pass an object instead of a hash to methods like new or update_attributes.

For example:

update_attributes(params[:image]) # error
update_attributes(:image => params[:image]) # no error

Hi croaky,

Thanks for your quick response. I have updated my issue to include controller code. Please have a look and help me.

Thanks again

Contributor

croaky commented Oct 8, 2012

Is the error in the create action? That location: @company looks a little fishy.

format.json { render json: @company, status: :created, location: @company }

Maybe add your controller spec code to this issue, too? What's the line number on the undefined methodstringify_keys' for #User:0xc9a70c0`? Have you tried commenting out or altering lines in the backtrace to find exactly where the problem is occurring?

I have a method in my controller spec to create session

def valid_session
   sign_in
end

The above method is throwing the error

undefined method stringify_keys' for #User:0xc9a70c0

due to which all test fails, because i am checking the session in every test by above method call.

Contributor

croaky commented Feb 13, 2013

I'm unable to reproduce this.

@croaky croaky closed this Feb 13, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment