{"id":716,"date":"2022-08-30T15:05:59","date_gmt":"2022-08-30T15:05:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/devise-undefined-local-variable-or-method-current_user-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:59","modified_gmt":"2022-08-30T15:05:59","slug":"devise-undefined-local-variable-or-method-current_user-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/devise-undefined-local-variable-or-method-current_user-collection-of-common-programming-errors\/","title":{"rendered":"Devise, undefined local variable or method current_user-Collection of common programming errors"},"content":{"rendered":"<p>I have <code>Project<\/code> controller and rspec file for it. I&#8217;m usig <code>current_user<\/code> helper in this way:<\/p>\n<pre><code># projects_controller_spec.rb\n\ndescribe \"user signed in\" do\n  before(:each) { sign_in @user }\n  after(:each)  { sign_out @user}\n\n  describe \"GET index\" do      \n    it \"assigns all projects as @projects\" do\n      project = FactoryGirl.create(:project, :user =&gt; current_user)\n      get :index, {}, valid_session\n      assigns(:projects).should eq([project])\n    end\n  end\n  ...\nend\n<\/code><\/pre>\n<p>The helpers <code>sign_in<\/code> and <code>sign_out<\/code> seem to work fine but I&#8217;v got an error on <code>current_user<\/code>:<\/p>\n<pre><code>undefined local variable or method `current_user'\n<\/code><\/pre>\n<p>What might be a problem, how to fix it?<\/p>\n<ol>\n<li>\n<p>It&#8217;s normal. You should create a User instance. You could use let like this :<\/p>\n<pre><code># projects_controller_spec.rb\n\ndescribe \"user signed in\" do\n\n  let(:user) {FactoryGirl.create(:user)}\n  before(:each) { sign_in user }\n  after(:each)  { sign_out user}\n\n  describe \"GET index\" do      \n    it \"assigns all projects as @projects\" do\n      project = FactoryGirl.create(:project, :user =&gt; user)\n      get :index, {}, valid_session\n      assigns(:projects).should eq([project])\n    end\n  end\n  ...\nend\n<\/code><\/pre>\n<p>You know than the current_user is user &#8230;<\/p>\n<p>You don&#8217;t need to sign out your use after each spec.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:43:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have Project controller and rspec file for it. I&#8217;m usig current_user helper in this way: # projects_controller_spec.rb describe &#8220;user signed in&#8221; do before(:each) { sign_in @user } after(:each) { sign_out @user} describe &#8220;GET index&#8221; do it &#8220;assigns all projects as @projects&#8221; do project = FactoryGirl.create(:project, :user =&gt; current_user) get :index, {}, valid_session assigns(:projects).should eq([project]) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=716"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/716\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}