{"id":6111,"date":"2014-04-13T02:24:26","date_gmt":"2014-04-13T02:24:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/rails-3-2-application-controller-devise-current_user-issue-collection-of-common-programming-errors\/"},"modified":"2014-04-13T02:24:26","modified_gmt":"2014-04-13T02:24:26","slug":"rails-3-2-application-controller-devise-current_user-issue-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/rails-3-2-application-controller-devise-current_user-issue-collection-of-common-programming-errors\/","title":{"rendered":"Rails 3.2 &#8211; Application Controller + Devise Current_user Issue-Collection of common programming errors"},"content":{"rendered":"<p>I have two roles defined in my User Model:<\/p>\n<pre><code>User.role == admin\nor\nUser.role == basic\n<\/code><\/pre>\n<p>In my <strong>Application Controller<\/strong>:<\/p>\n<pre><code>include MobilizedController\n<\/code><\/pre>\n<p>In <strong>lib\/mobilized_controller.rb<\/strong>:<\/p>\n<pre><code>module MobilizedController\n  extend ActiveSupport::Concern\n\n  included do\n    before_filter :set_basic_request, :if =&gt; :basic_logged_in?\n  end \n\n  private\n\n  def set_basic_request\n    request.format = :basic\n    prepend_view_path \"app\/views\/basic\"\n  end \n\n  def basic_logged_in?\n    current_user.role == 'basic'\n  end \nend\n<\/code><\/pre>\n<p>This is all fine when I am logged in, it sets the Mime Type correctly and renders my basic views. Unfortunately, when I&#8217;m not logged in I get:<\/p>\n<blockquote>\n<p>Undefined method &#8216;role&#8217; for nil:NilClass<\/p>\n<\/blockquote>\n<p>which I guess means that the current_user is not set, and thus calling nil on it won&#8217;t work.<\/p>\n<p>Does anyone have any suggestion for this? I need to be able to render the default log in page if a user is not logged in, and then set the Mime Type on login.<\/p>\n<p>Any help?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have two roles defined in my User Model: User.role == admin or User.role == basic In my Application Controller: include MobilizedController In lib\/mobilized_controller.rb: module MobilizedController extend ActiveSupport::Concern included do before_filter :set_basic_request, :if =&gt; :basic_logged_in? end private def set_basic_request request.format = :basic prepend_view_path &#8220;app\/views\/basic&#8221; end def basic_logged_in? current_user.role == &#8216;basic&#8217; end end This is all [&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-6111","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6111","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=6111"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6111\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}