{"id":248,"date":"2022-08-30T14:58:11","date_gmt":"2022-08-30T14:58:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/factorygirl-rspec-actionviewtemplateerror-undefined-method-for-nilnilclass-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:58:11","modified_gmt":"2022-08-30T14:58:11","slug":"factorygirl-rspec-actionviewtemplateerror-undefined-method-for-nilnilclass-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/factorygirl-rspec-actionviewtemplateerror-undefined-method-for-nilnilclass-record-and-share-programming-errors\/","title":{"rendered":"FactoryGirl Rspec ActionView::Template::Error: undefined method for nil:NilClass-Record and share programming errors"},"content":{"rendered":"<p>I&#8217;m new to rails and am trying to give TDD a try.<\/p>\n<p>I have a user model that has an admin attribute that is set to nil by default and a request model.<\/p>\n<p>Here&#8217;s a test that I have for my request controller<\/p>\n<pre><code>it \"should grant access to 'destroy'\" do\n        req = Factory(:request, :user =&gt; @user)\n        delete :destroy, :id =&gt; req.id\n        response.should be_successful\nend\n<\/code><\/pre>\n<p>When I run this I get the following error:<\/p>\n<pre><code>ActionView::Template::Error:undefined method `admin' for nil:NilClass\n<\/code><\/pre>\n<p>I&#8217;m guessing this is because my views have links that only show up if the user owns the link or if they are an admin. So, I&#8217;m doing conditional testing on the admin attribute. Do I need to set the admin attribute to false?<\/p>\n<p>How do I deal with this?<\/p>\n<ol>\n<li>\n<p>I made a mistake. I tried to call user.admin in my controller when the user was nil. I created a helper method to check if user was nil before checking the admin field.<\/p>\n<pre><code>def admin?(user)\n  if not user.nil?\n    return user.admin\n  end\n  return false\nend\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-08-31 06:18:26. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m new to rails and am trying to give TDD a try. I have a user model that has an admin attribute that is set to nil by default and a request model. Here&#8217;s a test that I have for my request controller it &#8220;should grant access to &#8216;destroy'&#8221; do req = Factory(:request, :user =&gt; [&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-248","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/248","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=248"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}