2016-07-28 67 views
0

我正在一个超级简单的通讯应用程序,我对这个错误感到困惑。为什么有nil课程?我只是要求它渲染,所以为什么我不能把redirect_to调用render我收到错误未定义的方法'空?'为零:NilClass

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

为什么代码<%= render '/' %>触发这部分的undefined method 'empty?' for nil:NilClass错误?

+0

你想用'render'/''实现什么? –

+0

我希望它回到主页 –

回答

0

因为你的愿望是给用户返回到主页,而不是

render '/' 

你应该使用

redirect_to root_path 

不同的是render准备输出显示为结果当前请求和redirect_to命令用户的浏览器在指定的URL请求新的请求。

虽然可以在任意操作中呈现主页的内容,但这种情况很少需要。一个缺点是页面网址不会自动更新到浏览器地址栏中的网站根目录。

请注意,render '/'不是正确的语法。 rendergenerally accepts选项的散列而不是字符串。

+0

它的工作!谢谢 –

0

为什么你有没有任何条件循环的<%else%>子句?你打算用<%渲染'/'%>吗?你是否打电话给索引页?如果是这样,你可以指定<%渲染“索引”%>。

+0

对不起,有一个if条件,我没有在代码中正确包含它,仍然得到相同的错误,虽然 –

0

所以你不需要如果和其他块在视图方面页面重定向到根路径。 从你的控制器的任何操作你需要下面的代码。

if admin_signed_in? redirect_to root_path end

还删除if和else block from view。

相关问题