当我尝试点击我的应用程序的wiki show视图时出现以下错误,但仅当我没有以用户身份登录时(我希望wiki show视图公开),我不能找出原因(它在<% if current_user.premium? %>
显示错误):
Wiki#show中的NoMethodError 显示/home/vagrant/code/Blocipedia/app/views/wikis/show.html.erb,其中第11行引发: 未定义的方法`premium?&#39;为零:NilClass
<% if current_user.premium? %>
<%= link_to "Add/Remove Collaborators", wiki_collaborators_path(@wiki), class: 'btn btn-info' %>
<% end %>
<p><%= @wiki.body %></p>
app / views / wikis / show.html.erb:11:在`_app_views_wikis_show_html_erb ___ 543246191_90269080&#39;
以下是我的服务器日志中的错误:
Started GET "/wikis/now-with-title" for 10.0.2.2 at 2014-09-26 14:50:28 +0000
Processing by WikisController#show as HTML
Parameters: {"id"=>"now-with-title"}
Wiki Load (1.5ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."slug" = 'now-with-title' ORDER BY created_at DESC LIMIT 1
Rendered wikis/show.html.erb within layouts/application (12.8ms)
Completed 500 Internal Server Error in 54ms
ActionView::Template::Error (undefined method `premium?' for nil:NilClass):
8: <%= link_to "Delete Wiki", @wiki, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this wiki?' } %>
9: <% end %>
10:
11: <% if current_user.premium? %>
12: <%= link_to "Add/Remove Collaborators", wiki_collaborators_path(@wiki), class: 'btn btn-info' %>
13: <% end %>
14: <p><%= @wiki.body %></p>
app/views/wikis/show.html.erb:11:in `_app_views_wikis_show_html_erb___271164051_80046150'
4.0.5/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.8ms)
这是我的维基节目视图文件:
<div class="row">
<div class="col-md-8">
<h1><%= @wiki.title %></h1>
<% if current_user %>
<%= link_to "Edit", edit_wiki_path(@wiki), class: 'btn btn-success' %>
<%= link_to "Delete Wiki", @wiki, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this wiki?' } %>
<% end %>
<% if current_user.premium? %>
<%= link_to "Add/Remove Collaborators", wiki_collaborators_path(@wiki), class: 'btn btn-info' %>
<% end %>
<p><%= @wiki.body %></p>
</div>
这是我的应用程序布局文件:
<!DOCTYPE html>
<html>
<head>
<title>Blocipedia</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="container">
<ul class="nav nav-tabs">
<li><%= link_to "Blocipedia", root_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Wikis", wikis_path %></li>
<div class="pull-right user-info">
<% if current_user %>
Hello <%= link_to current_user.email, edit_user_registration_path %>!
<%= link_to edit_user_registration_path, class: 'btn btn-primary btn-sm' do %>
<span class="glyphicon glyphicon-user"></span>
<% end %>
<%= link_to destroy_user_session_path, method: :delete, class: 'btn btn-primary btn-sm' do %>
<span class="glyphicon glyphicon-log-out"></span>
<% end %>
<% else %>
<%= link_to "Sign In", new_user_session_path %> or
<%= link_to "Sign Up", new_user_registration_path %>
<% end %>
</div>
</ul>
<% if flash[:notice] %>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash[:notice] %>
</div>
<% elsif flash[:error] %>
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash[:error] %>
</div>
<% elsif flash[:alert] %>
<div class="alert alert-warning">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash[:alert] %>
</div>
<% end %>
<%= yield %>
</body>
</html>
这是我的wiki控制器:
class WikisController < ApplicationController
def index
@wikis = Wiki.all
end
def show
@wiki = Wiki.friendly.find(params[:id])
end
def new
@wiki = Wiki.new
end
def create
@wiki = current_user.wikis.build(wiki_params)
if @wiki.save
flash[:notice] = "Wiki was saved."
redirect_to @wiki
else
flash[:error] = "There was an error saving the wiki. Please try again."
render :new
end
end
def edit
@wiki = Wiki.friendly.find(params[:id])
end
def update
@wiki = Wiki.friendly.find(params[:id])
if @wiki.update_attributes(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to @wiki
else
flash[:error] = "There was an error saving the wiki. Please try again."
render :edit
end
end
def destroy
@wiki = Wiki.friendly.find(params[:id])
title = @wiki.title
if @wiki.destroy
flash[:notice] = "Wiki was deleted successfully."
redirect_to wikis_path
else
flash[:error] = "There was an error deleting the wiki."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body)
end
end
提前感谢您的帮助!
答案 0 :(得分:1)
当用户未登录时,current_user
为零。因此,您突出显示的行与if nil.premium?
相同,当然nil
没有附加premium?
方法。
我会将条件更改为if current_user && current_user.premium?
。