[英]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? 为什么没有nil
班? I am only asking it to render, so why cant I put a redirect_to
where the render
call is?. 我只要求它渲染,所以为什么不能将redirect_to
放到render
调用所在的位置?
<% 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
为什么这部分代码<%= render '/' %>
触发undefined method 'empty?' for nil:NilClass
undefined method 'empty?' for nil:NilClass
error? undefined method 'empty?' for nil:NilClass
错误?
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. 区别在于render
准备将输出显示为当前请求的结果,而redirect_to
命令用户的浏览器在指定的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. 缺点之一是页面URL不会自动更新到浏览器地址栏中的站点根目录。
As a side note, render '/'
is not a correct syntax. 附带说明, render '/'
不是正确的语法。 render
generally accepts a hash of options and not a string. render
通常接受选项的哈希,而不是字符串。
Why do you have an <% else %> clause without any conditional loops? 为什么您有一个<%else%>子句而没有任何条件循环? And what are you trying to call with <% render '/' %>? 您想用<%render'/'%>调用什么? Are you trying to call the index page? 您是否要调用索引页面? If so, you can specify by saying <% render "index" %> . 如果是这样,可以通过说<%render“ index”%>来指定。
So you don't need if and else block in view side for page redirect to root path. 因此,您不需要if和else在视图侧进行阻塞,即可将页面重定向到根路径。 From your controller's whatever action you need following code. 从控制器的任何操作中,您都需要以下代码。
if admin_signed_in? 如果是admin_signed_in? redirect_to root_path end redirect_to root_path结束
also remove if and else block from view. 还将if和else挡在视野之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.