Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get ActionView::Template::Error: undefined method `name' for nil:NilClass on Heroku but not locally
    primarykey
    data
    text
    <p>I have a Rails 4 app running SQLite locally and PostgreSQL on Heroku. </p> <p>I am having problems with the index action of my PostController. I get the above error, but only on Heroku.</p> <p>I have double checked all the code and run all migrations and restarted the Heroku server. I have also double checked that the migrations are identical both locally and on Heroku. The issue seems to be specifically related to accessing data in the User model through the Post model. For instance, I've tried to access other attributes such as email through the rails console on Heroku but I get the same undefined method error. So I'm pretty sure it has something to do with the table join.</p> <p>Also to clarify, I do have data in the production DB</p> <p>Here's my PostsController action:</p> <pre><code>class PostsController &lt; ApplicationController before_filter :authenticate_user!, except: [:index, :show] before_action :set_post, only: [:show, :edit, :update, :destroy] # GET /posts # GET /posts.json def index @posts = Post.all end end </code></pre> <p>My Post model:</p> <pre><code>class Post &lt; ActiveRecord::Base belongs_to :user end </code></pre> <p>My code in the User model:</p> <pre><code>class User &lt; ActiveRecord::Base rolify # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable belongs_to :qualification has_many :posts validates :phone, presence: true, format: { with: /\d{3}.\d{3}.\d{4}/, message: "Must be in 555.555.5555 format" } validates :name, presence: true validates :email, presence: true validates :acknowledgement, presence: true # new function to set the password without knowing the current password used in our confirmation controller. def attempt_set_password(params) p = {} p[:password] = params[:password] p[:password_confirmation] = params[:password_confirmation] update_attributes(p) end # new function to return whether a password has been set def has_no_password? self.encrypted_password.blank? end # new function to provide access to protected method unless_confirmed def only_if_unconfirmed pending_any_confirmation {yield} end def password_required? # Password is required if it is being set, but not for new records if !persisted? false else !password.nil? || !password_confirmation.nil? end end end </code></pre> <p>And my index.html.erb file:</p> <pre><code>&lt;% @posts.each do |post| %&gt; &lt;h2 class='subheader'&gt;&lt;%= link_to post.title, post %&gt;&lt;/h2&gt; &lt;h6&gt;written by &lt;%= post.user.name %&gt; on &lt;%= post.created_at.to_s(:long) %&gt;&lt;/h6&gt; &lt;div&gt;&lt;%= post.body.html_safe %&gt;&lt;/div&gt; &lt;% if user_signed_in? %&gt; &lt;% if current_user.has_role? :admin %&gt; &lt;div&gt;&lt;%= link_to 'Edit', edit_post_path(post) %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;br&gt; &lt;%= link_to 'New Post', new_post_path %&gt; </code></pre> <p>The output from my Heroku log:</p> <pre><code>2013-12-19T06:31:13.280899+00:00 app[web.1]: Started GET "/posts" for 64.114.175.126 at 2013-12-19 06:31:13 +0000 2013-12-19T06:31:13.280899+00:00 app[web.1]: Started GET "/posts" for 64.114.175.126 at 2013-12-19 06:31:13 +0000 2013-12-19T06:31:13.283663+00:00 app[web.1]: Processing by PostsController#index as HTML 2013-12-19T06:31:13.283663+00:00 app[web.1]: Processing by PostsController#index as HTML 2013-12-19T06:31:13.292344+00:00 app[web.1]: Rendered posts/index.html.erb within layouts/application (6.9ms) 2013-12-19T06:31:13.292553+00:00 app[web.1]: Completed 500 Internal Server Error in 9ms 2013-12-19T06:31:13.292344+00:00 app[web.1]: Rendered posts/index.html.erb within layouts/application (6.9ms) 2013-12-19T06:31:13.292553+00:00 app[web.1]: Completed 500 Internal Server Error in 9ms 2013-12-19T06:31:13.295581+00:00 app[web.1]: 2013-12-19T06:31:13.295581+00:00 app[web.1]: 6: &lt;div&gt;&lt;%= post.body.html_safe %&gt;&lt;/div&gt; 2013-12-19T06:31:13.295581+00:00 app[web.1]: 3: &lt;% @posts.each do |post| %&gt; 2013-12-19T06:31:13.295581+00:00 app[web.1]: 7: &lt;% if user_signed_in? %&gt; 2013-12-19T06:31:13.295581+00:00 app[web.1]: 2: 2013-12-19T06:31:13.295581+00:00 app[web.1]: 4: &lt;h2 class='subheader'&gt;&lt;%= link_to post.title, post %&gt;&lt;/h2&gt; 2013-12-19T06:31:13.295581+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for nil:NilClass): 2013-12-19T06:31:13.295581+00:00 app[web.1]: 5: &lt;h6&gt;written by &lt;%= post.user.name %&gt; on &lt;%= post.created_at.to_s(:long) %&gt;&lt;/h6&gt; 2013-12-19T06:31:13.295778+00:00 app[web.1]: app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb__338969566965495969_70155247178440' 2013-12-19T06:31:13.295778+00:00 app[web.1]: 2: 2013-12-19T06:31:13.295778+00:00 app[web.1]: 2013-12-19T06:31:13.295778+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for nil:NilClass): 2013-12-19T06:31:13.295778+00:00 app[web.1]: 2013-12-19T06:31:13.295778+00:00 app[web.1]: 3: &lt;% @posts.each do |post| %&gt; 2013-12-19T06:31:13.295778+00:00 app[web.1]: 2013-12-19T06:31:13.295581+00:00 app[web.1]: 8: &lt;% if current_user.has_role? :admin %&gt; 2013-12-19T06:31:13.295581+00:00 app[web.1]: app/views/posts/index.html.erb:5:in `block in _app_views_posts_index_html_erb__338969566965495969_70155247178440' 2013-12-19T06:31:13.295778+00:00 app[web.1]: 5: &lt;h6&gt;written by &lt;%= post.user.name %&gt; on &lt;%= post.created_at.to_s(:long) %&gt;&lt;/h6&gt; 2013-12-19T06:31:13.295778+00:00 app[web.1]: 6: &lt;div&gt;&lt;%= post.body.html_safe %&gt;&lt;/div&gt; 2013-12-19T06:31:13.295947+00:00 app[web.1]: 7: &lt;% if user_signed_in? %&gt; 2013-12-19T06:31:13.295947+00:00 app[web.1]: 8: &lt;% if current_user.has_role? :admin %&gt; 2013-12-19T06:31:13.295947+00:00 app[web.1]: app/views/posts/index.html.erb:5:in `block in _app_views_posts_index_html_erb__338969566965495969_70155247178440' 2013-12-19T06:31:13.295947+00:00 app[web.1]: app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb__338969566965495969_70155247178440' 2013-12-19T06:31:13.295778+00:00 app[web.1]: 4: &lt;h2 class='subheader'&gt;&lt;%= link_to post.title, post %&gt;&lt;/h2&gt; 2013-12-19T06:31:13.295947+00:00 app[web.1]: 2013-12-19T06:31:13.295947+00:00 app[web.1]: 2013-12-19T06:31:13.303637+00:00 heroku[router]: at=info method=GET path=/posts host=www.kettlecreekventure.com fwd="64.114.175.126" dyno=web.1 connect=8ms service=25ms status=500 bytes=1266 2013-12-19T06:31:14.630725+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=www.kettlecreekventure.com fwd="64.114.175.126" dyno=web.1 connect=1ms service=4ms status=200 bytes=0 2013-12-19T06:31:22.445573+00:00 heroku[run.7634]: Process exited with status 0 2013-12-19T06:31:22.459444+00:00 heroku[run.7634]: State changed from up to complete </code></pre> <p>Again, the code works perfectly on my local machine but fails with the above error after I have deployed to Heroku. There error is in the index.html.erb file when the app tries to reference post.user.name. I'm pulling my hair out trying to figure it out. I would appreciate if anyone can see where my problem is. What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload