简体   繁体   中英

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 undefined method 'empty?' for nil:NilClass error?

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.

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" %> .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM