Dotun Longe
Dotun Longe

Reputation: 23

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 nil class? I am only asking it to render, so why cant I put a redirect_to where the render call is?.

 <% if admin_signed_in? %>

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

   <h1>Subscribedusers</h1>

   <table>
 <thead>
  <tr>
    <th>Email</th>
    <th colspan="3"></th>
  </tr>
</thead>

<tbody>
<% @subscribedusers.each do |subscribeduser| %>
  <tr>
    <td><%= subscribeduser.email %></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>
  </tr>
  <% end %>
</tbody>
</table>

<br>

<%= 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 error?

Upvotes: 1

Views: 754

Answers (3)

Rajnik
Rajnik

Reputation: 83

So you don't need if and else block in view side for page redirect to root path. From your controller's whatever action you need following code.

if admin_signed_in? redirect_to root_path end

also remove if and else block from view.

Upvotes: 0

Nic Nilov
Nic Nilov

Reputation: 5156

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.

Upvotes: 2

PS.
PS.

Reputation: 343

Why do you have an <% else %> clause without any conditional loops? And what are you trying to call with <% render '/' %>? Are you trying to call the index page? If so, you can specify by saying <% render "index" %> .

Upvotes: 0

Related Questions