简体   繁体   English

我正在获取错误的未定义方法“空”? 对于nil:NilClass

[英]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.

相关问题 为什么我得到nil:NilClass的未定义方法“ destroy”? - why am i getting undefined method `destroy' for nil:NilClass? 为什么我在 Rails 中为 nil:NilClass 得到未定义的方法“each”? - Whay am I getting undefined method `each' for nil:NilClass in Rails? 为什么我得到 nil:NilClass 的未定义方法“错误”? - Why am I getting an undefined method `errors' for nil:NilClass? 为什么即使在Rails中有一个ID,我仍会收到错误“ nil:NilClass的未定义方法&#39;id&#39;”? - Why am I getting error “undefined method `id' for nil:NilClass” even though there's a id in Rails? 什么是“通道”以及它在哪里。我正在为nil:NilClass获取此错误未定义的方法“ channels” - what is “channels” and where it is comming .i am getting this error undefined method `channels' for nil:NilClass 我在Rails中获取nil:NilClass的错误未定义方法“ to_sym”以更新图像? - I am getting error undefined method `to_sym' for nil:NilClass in Rails for updating image? 明显:错误:未定义方法“空?” 对于nil:NilClass - Filterrific: ERROR: undefined method `empty?' for nil:NilClass 为什么我收到“ nil:NilClass的未定义方法&#39;map&#39;”错误 - Why I'm getting “undefined method `map' for nil:NilClass” error 为nil:NilClass错误获取未定义的方法“ +” - Getting an undefined method `+' for nil:NilClass error 为nil:NilClass获取错误未定义方法`[]&#39;(NoMethodError) - Getting error undefined method `[]' for nil:NilClass (NoMethodError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM