0

更新 我解决了。代码中的 if 语句存在一些问题

在本地一切正常,但我不确定为什么会出现错误

我有我的 rails 应用程序,我正在尝试访问 URL“/users/username”,例如 tbrown。它是由未定义的方法“admin”还是其他原因引起的?Admin_user 已在我的 application_controller.rb 中定义,并且我有一个 admin_user.rb 模型。在这里发布的是heroku日志和控制器

heroku 日志

2013-03-20T08:05:55+00:00 app[web.1]: Started GET "/users/tbrown" for xx.xx.xx
.xx at 2013-03-20 08:05:55 +0000
2013-03-20T08:05:55+00:00 app[web.1]:
2013-03-20T08:05:56+00:00 app[web.1]:
2013-03-20T08:05:56+00:00 app[web.1]:     22:     <% end %></div>
2013-03-20T08:05:56+00:00 app[web.1]: ActionView::Template::Error (undefined met
hod `admin?' for nil:NilClass):
2013-03-20T08:05:56+00:00 app[web.1]:     23:     <% if current_user.admin? %>
 2013-03-20T08:05:56+00:00 app[web.1]:     21:     <span class="content2"><%= com
ment.comment_content %></span>
2013-03-20T08:05:56+00:00 app[web.1]:     24:     <%= link_to "delete", pos
t, method: :delete,

应用程序控制器

def admin_user?
    unless current_user && current_user.admin?
      redirect_to root_url
      return false
    end
  end
4

1 回答 1

2

问题出在你app/views/posts/_post.html.erb的线上:

<% if current_user.admin? %>

This assumes that current_user is not nil (i.e. a user is logged in). You are already defensively coding around that problem in your controller admin_user? method. It might be better to implement a helper method along the same lines (but without the redirect) and call that from your view instead.

于 2013-03-20T08:39:56.363 回答