Dotun Longe Dotun Longe - 1 year ago 118
Ruby Question

I am Getting the error undefined method `empty?' for nil:NilClass

I am working on a super simple newsletter application, and I'm confused about this error. Why is there a

class? I am only asking it to render, so why cant I put a
where the
call is?.

<% if admin_signed_in? %>

<p id="notice"><%= notice %></p>


<th colspan="3"></th>

<% @subscribedusers.each do |subscribeduser| %>
<td><%= %></td>
<td><%= link_to 'Show', subscribeduser %></td>
<td><%= link_to 'Edit', edit_subscribeduser_path(subscribeduser) %></td>
<td><%= link_to 'Destroy', subscribeduser, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>


<%= link_to 'New Subscribeduser', new_subscribeduser_path %>

<% else %>

<%= render '/' %>

<% end %>

Why does this part of the code
<%= render '/' %>
trigger an
undefined method 'empty?' for nil:NilClass

Answer Source

Since your desire is to return the user to the home page, instead of

render '/'

you should use

redirect_to root_path

The difference is render prepares the output to be displayed as the result of the current request and redirect_to commands user's browser to make a new request at specified url.

While it can be possible to render the contents of your home page in an arbitrary action, this is rarely desirable. One downside is the page url would not automatically update to your site's root in the browser's address line.

As a side note, render '/' is not a correct syntax. render generally accepts a hash of options and not a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download