1

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?

2
  • What are you trying to achieve with render '/'? Commented Jul 28, 2016 at 22:30
  • i want it to return to the homepage Commented Jul 29, 2016 at 14:00

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Sorry, there is an if condition, i did not include it in the code properly, still get the same error though
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.