{"id":925,"date":"2022-08-30T15:09:28","date_gmt":"2022-08-30T15:09:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/rails-3-only-rendering-partial-if-a-user-is-logged-in-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:28","modified_gmt":"2022-08-30T15:09:28","slug":"rails-3-only-rendering-partial-if-a-user-is-logged-in-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rails-3-only-rendering-partial-if-a-user-is-logged-in-collection-of-common-programming-errors\/","title":{"rendered":"Rails 3, only rendering partial if a user is logged in-Collection of common programming errors"},"content":{"rendered":"<p>On a Rails 3.1 app, I have a method called <code>user_logged_in?<\/code> from Devise, which I want to use to render a partial selectively. Here&#8217;s the code in layouts\/application.html.erb:<\/p>\n<pre><code>\n \"home\/statusbar\" %&gt;\n\n<\/code><\/pre>\n<p>It uses a partial from a different controller. This is the partial:<\/p>\n<pre><code>  \n  Signed in as \n\n<\/code><\/pre>\n<p>An error occurs at <code>current_user.name<\/code> (undefined method), because <code>current_user<\/code> is <code>nil<\/code>. I could move the <code>user_logged_in?<\/code> check inside the partial, but I think it would be marginally more efficient if it were in the view.<\/p>\n<p>Could you direct me to what the best practices are, or how I can overcome the undefined method error? Is it normal that Rails checks the code of the template even though it&#8217;s in an <code>if<\/code> block that fails?<\/p>\n<ol>\n<li>\n<p>It looks like <code>user_logged_in?<\/code> is where something is going wrong.Does it work if you change to this?<\/p>\n<pre><code>\n   \"home\/statusbar\" %&gt;\n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I&#8217;m familiar with Devise, but not the <code>user_logged_in?<\/code> method &#8211; did you define it yourself? The normal helper used in views is <code>user_signed_in?<\/code>. Also, try something like the following:<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p>Above the if statement in your view, and check your logs to make sure your call is returning <code>false<\/code> correctly (<code>user_signed_in?<\/code> should also be returning <code>false<\/code>). You should not be getting a nil error in that partial unless it is actually being rendered (meaning the code entered the if statement).<\/p>\n<\/li>\n<li>\n<p>That code is being run, ruby is duck typed and dynamic, all checks are at runtime. So you need to check your conditional . Try<\/p>\n<pre><code>\n   \"home\/statusbar\" %&gt;\n\n<\/code><\/pre>\n<p>and see if it stll fails. A bettter solution would be<\/p>\n<pre><code>\n   \"home\/statusbar\" %&gt;\n\n<\/code><\/pre>\n<p>I am unfamiliar with devise but this should work Since current user wil be nil if nobody is logged in<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:58:49. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>On a Rails 3.1 app, I have a method called user_logged_in? from Devise, which I want to use to render a partial selectively. Here&#8217;s the code in layouts\/application.html.erb: &#8220;home\/statusbar&#8221; %&gt; It uses a partial from a different controller. This is the partial: Signed in as An error occurs at current_user.name (undefined method), because current_user is [&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-925","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/925","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=925"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}