current_user 登录引发 NoMethodError 后被视为 nil
current_user considered nil after login raises NoMethodError
登录后我收到一个动作控制器异常:
StaticPages#home 中没有方法错误
nil:NilClass
的未定义方法 'email'
认为 nil 的是 current_user
以下第 12 行 _comment.html.erb
部分:
<li id="comment-<%= comment.id %>">
<%= link_to gravatar_for(comment.user, size: 30), comment.user %>
<span class="comment-content"><%= link_to comment.user.name, comment.user %> <%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago.
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete, remote: true %>
<% end %>
</span>
<% if logged_in? && (current_user == comment.user || current_user = comment.micropost.user) %>
<div class="reply-section">
<%= link_to gravatar_for(current_user, size: 20), current_user %>
<%= form_for(current_user.replies.build) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div><%= f.hidden_field :comment_id, value: comment.id %></div>
<div class="reply-box">
<%= f.text_area :content, rows: "1", class: "reply_area" %>
</div>
<%= f.submit "Reply", class: "btn btn-primary btn-xs" %>
<% end %>
</div>
<% end %>
<div class="replies-section">
<% if comment.replies.any? %>
<ol id="replies_comment-<%= comment.id %>">
<% comment.replies.each do |reply| %>
<%= render reply %>
<% end %>
</ol>
<% end %>
</div>
</li>
异常邮箱为gravatar_for
定义中的邮箱。
如果我删除第 12 行,引发的异常是 undefined method 'replies' for nil:NilClass,即下一行中的 current_user
。
上面的部分作为 <%= render comment %>
插入到 micropost 部分中。我试图直接在 micropost 部分中添加代码,但无论如何都会引发异常。
抛出异常的代码属于回复功能:用户可以post微博post、微博评论post、回复微博post评论。
我不明白为什么current_user
登录后被认为是nil。
current_user
在session helper中定义如下:
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
条件语句上方两行,if logged_in?
只检查 current_user
不为零,因为方法 logged_in?
被定义为 !current_user.nil?
此外,current_user
在插入注释部分的 micropost 部分中被多次调用,但 rails 在该点开始引发异常。
您的情况:
<% if logged_in? && (current_user == comment.user || current_user = comment.micropost.user) %>
您正在分配 current_user
而不是检查它,这是导致问题的原因:
current_user = comment.micropost.user
应该有 ==
而不是 =
并且 comment.micropost.user
是 nil 分配给 current_user
并且它抛出异常。
你这里有错误:
<% if logged_in? && (current_user == comment.user || current_user = comment.micropost.user) %>
您正在将 comment.micropost.user
分配给 current_user
,而不是比较它的值。
所以,当到达那个点时,你会得到一个错误,因为 current_user
值是用你在 comment.micropost.user
中的任何值设置的,并且由于它不是有效的 id
你得到nil
来自 current_user
.
解决问题只需将=
更改为==
:
<% if logged_in? && (current_user == comment.user || current_user == comment.micropost.user) %>
登录后我收到一个动作控制器异常:
StaticPages#home 中没有方法错误
nil:NilClass
认为 nil 的是 current_user
以下第 12 行 _comment.html.erb
部分:
<li id="comment-<%= comment.id %>">
<%= link_to gravatar_for(comment.user, size: 30), comment.user %>
<span class="comment-content"><%= link_to comment.user.name, comment.user %> <%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago.
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete, remote: true %>
<% end %>
</span>
<% if logged_in? && (current_user == comment.user || current_user = comment.micropost.user) %>
<div class="reply-section">
<%= link_to gravatar_for(current_user, size: 20), current_user %>
<%= form_for(current_user.replies.build) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div><%= f.hidden_field :comment_id, value: comment.id %></div>
<div class="reply-box">
<%= f.text_area :content, rows: "1", class: "reply_area" %>
</div>
<%= f.submit "Reply", class: "btn btn-primary btn-xs" %>
<% end %>
</div>
<% end %>
<div class="replies-section">
<% if comment.replies.any? %>
<ol id="replies_comment-<%= comment.id %>">
<% comment.replies.each do |reply| %>
<%= render reply %>
<% end %>
</ol>
<% end %>
</div>
</li>
异常邮箱为gravatar_for
定义中的邮箱。
如果我删除第 12 行,引发的异常是 undefined method 'replies' for nil:NilClass,即下一行中的 current_user
。
上面的部分作为 <%= render comment %>
插入到 micropost 部分中。我试图直接在 micropost 部分中添加代码,但无论如何都会引发异常。
抛出异常的代码属于回复功能:用户可以post微博post、微博评论post、回复微博post评论。
我不明白为什么current_user
登录后被认为是nil。
current_user
在session helper中定义如下:
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
条件语句上方两行,if logged_in?
只检查 current_user
不为零,因为方法 logged_in?
被定义为 !current_user.nil?
此外,current_user
在插入注释部分的 micropost 部分中被多次调用,但 rails 在该点开始引发异常。
您的情况:
<% if logged_in? && (current_user == comment.user || current_user = comment.micropost.user) %>
您正在分配 current_user
而不是检查它,这是导致问题的原因:
current_user = comment.micropost.user
应该有 ==
而不是 =
并且 comment.micropost.user
是 nil 分配给 current_user
并且它抛出异常。
你这里有错误:
<% if logged_in? && (current_user == comment.user || current_user = comment.micropost.user) %>
您正在将 comment.micropost.user
分配给 current_user
,而不是比较它的值。
所以,当到达那个点时,你会得到一个错误,因为 current_user
值是用你在 comment.micropost.user
中的任何值设置的,并且由于它不是有效的 id
你得到nil
来自 current_user
.
解决问题只需将=
更改为==
:
<% if logged_in? && (current_user == comment.user || current_user == comment.micropost.user) %>