ActionView::Template::Error (undefined method `empty?' for nil:NilClass): - ruby-on-rails

I'm deploying a fairly simple Rails 6.1 app to Heroku. I'm hitting this error on a deploy to Heroku but not locally. I've looked through all the previous posts related to this error, none are actually empty?, they usually are properties on objects that aren't in the database. I can't figure this one out. The error I'm receiving is not related to a database call. My home controller makes no calls to the database, the index page is simple and the error message I'm receiving has to do with path calls built into Rails:
ActionView::Template::Error (undefined method 'empty?' for nil:NilClass):
14: <!-- Inner -->
15: <ul class="sidenav-inner py-1">
16: <li class="sidenav-divider mt-0"></li>
17: <li class="sidenav-item<%= current_page?(root_path) ? ' active' : '' %>">
18: <%= link_to root_path, class: "sidenav-link" do %>
19: <i class="sidenav-icon fad fa-home"></i><div>Home</div>
20: <% end %>`
app/views/layouts/partials/_layout-sidenav.html.erb:17
app/views/layouts/main/layout-without-navbar.html.erb:9
app/views/layouts/main/layout-without-navbar.html.erb:1
My controller:
class HomeController < ApplicationController
def index
#title = 'Home'
end
end
My index page:
<h4 class="font-weight-bold py-3 mb-4">Home</h4>
<p>
This page is an example of basic layout.
</p>
<p>
<button class="btn btn-primary btn-lg">Button</button>
</p>
I've removed the line the error is on (17) and the same error gets thrown on the next line (18) calling root_path.
I've verified that my database migrations have run on Heroku.
I've seeded the Heroku database with sample data for all classes from seeds.rb. I normally wouldn't do this but I wanted to troubleshoot to see if there was an query returning nil somewhere.
I understand what the nil:NilClass error is, I'm attempting to call empty? on a class that's nil. For one, when I do a search through my code there are 0 results for empty? so it must be called in an underlying somewhere. Also, there are no database calls here, and why would a path be nil? I'm starting to feel like this is a red herring error. I am using Devise so I'm curious if it's somehow related to that and I haven't configured something correctly.
Any ideas anyone can give I'd appreciate.

For anyone who may hit this issue, I needed to add the following line in my production.rb file. Add your subdomain.
config.action_mailer.default_url_options = { :host => 'yourdomain.herokuapp.com' }

Related

nil' is not an ActiveModel-compatible object. It must implement :to_partial_path

Getting an error trying to run my Rails app. I think it might be a data issue based on a similar error I researched in Stack Overflow. Below is the output from the Command Line:
ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
2: <% provide(:blog_active, 'active') %>
3:
4: <div class="row">
5: <%= render #posts %>
6: </div>
app/views/blog/posts/index.html.erb:5:in `_app_views_blog_posts_index_html_erb__817338750_58788108'
Maybe there is a discrepancy with the name of your partial. Are you sure it is _posts.html.erb and not _post.html.erb?
I just found this Stack Overflow question that's related:
Rails rendering instance variable from application.html.erb
I had the same error. And my problem was with #posts variable in my PostsController #index. I made a mistake writing the #posts variable.
And when I fixed the #posts value, the problem was gone.

undefined method `lastname' for nil:NilClass rails - Avatar issues

OK, not sure what I did.. but it was working before and now it stopped.
I'm currently trying to add initials as the avatar to users' navbar, following this step by step guide
It all makes sense and I did what was required, but for some reason I'm now getting the error of undefined method..
I'm not sure what am I doing wrong to be honest...Obviosly, users have attribute of firstname and lastname.. Any help would be greatly appreciated. Thank you!
UsersHelper
module UsersHelper
def show_avatar(user)
render partial: 'users/avatar'
end
end
Avatar partial under users/_avatar
<div class=“avatar-circle-sm”>
<span class="initials">
<%= #user.lastname[0..1].upcase %>
</span>
</div>
Page navbar shared/_navbar
<div>
<%= show_avatar(#user) %>
</div>
As discussed in chat, since you're using Devise, you can use current_user instead of setting a new #user variable.

RoR undefined method 'to_datetime' depending on order of helper methods

I am experiencing a very odd problem; I get this error
undefined method `to_datetime' for nil:NilClass"
when render 'modal' is before render 'post' in show.html.erb, but not if it is after:
<div class="page-header">
<h2>Indlæg af <%= #user.first_name %></h2>
</div>
<% if current_user == #user %>
<%= render 'modal' %>
<% end %>
<%= render 'post' %>
I have attached the views in the gist underneath:
Gist: https://gist.github.com/czepluch/8166841
It makes no sense to me that this error occurs depending on the order the renders are placed in. So I would really like to know why the order of the rendering of the helpers matter?
Please add code to the question so it is saved with the answers here. I suspect it has to do with the fact that your form has this:
simple_form_for([#user, #user.posts.build]
That means a new post is built that has no attributes, which could cause a nil error later on. reverse the order, and this isn't built until after the other code has run. The only thing I can see at first glance that is related to datetime would be:
time_ago_in_words(p.created_at)
If created_at is nil, because of an empty post, that could generate an error.
Play with the form declaration, you may be able to do
simple_form_for([#user, :post])
or something like that to get a form for a new post without actually attaching an empty object to the #user object.

List all records on main page

I'm trying to list all records from the database in the main page.
Model and controllers were both created properly (I suspect) since using the following code and pointing to proper address (http://localhost:3000/subdomainw1s) lists all the records:
in /app/views/subdomainw1s/index.html.erb:
<h1>subdomain word 1</h1>
<ol class="subdomainw1">
<% #subdomainw1s.each do |sdw1| %>
<li>
<%= sdw1.blognamew1 %>
</li>
<% end %>
</ol>
However, attempting to past the same code in /app/views/home/index.html.erb results in an error message ("undefined method `each' for nil:NilClass") complaining about:
<% #subdomainw1s.each do |sdw1| %>
Clearly, rails doesn't know what to do with this model under the home page.. no?
Any advice would be great.
there is at least two more "codeless" options
convert it to (empty) array
..#subdomainw1s.to_a.each do |sdw1|..
or use method try
..#subdomainw1s.try(:each) do |sdw1|..
copy the code to collect the subdomains from 'index' in subdomains controller to 'index' in home controller.

Ruby On Rails - "undefined method `id' for 4:Fixnum"

I recently decided I wanted to list all the users in my Ruby On Rails application - since I couldn't figure out how to list them any other way, I decided to use partials.
I have the following on my administration page (just hooked up to a its own administration controller):
<%= render :partial => User.find(:all) %>
I then have a file called _user.html.erb in my users view folder. This contains the following:
<ul>
<% div_for #user.object_id do %>
<li><%= link_to user.username, user.username %></li>
<% end %>
</ul>
When the application runs and I go to the administration page, I get the following error:
undefined method `id' for 4:Fixnum
It says it's because of this line (which is in the partial file):
<% div_for #user.object_id do %>
I'm unsure why this happens (and have googled for hours to try and find results and only find solutions that don't work for me). I think it's something to do with my usage of the #user instance variable, but I'm not totally sure.
You get that error because div_for expects an active record object as an argument, which it calls the id method on. You pass in a fixnum (the result of #user.object_id), which is not an active record object and does not have an id method.
So pass in #user instead of #user.object_id and it will work.
Also you should use user instead of #user, rails 3 does not set instance variables for partials anymore.
Lose the .object_id part. I seriously can't think why are you using object_id!Do you have a good reason for doing so? Anyway div_for wraps a div around an object, so leave the .object_id part!
instead of object you are using it's column or visa varsa you are using at that time you will get this kind of error.
Example
I am using id instead of user = User.first object.
Try this, it worked for me.
#item.unit_of_measure.name
instead of
#item.unit_of_measure_id.name

Resources