我的邮政编码有问题,请看这个。 错误是说
Admin :: Posts #index中的NoMethodError显示 /home/muba/rblog/app/views/admin/posts/index.html.erb第2行 提出:
未定义的方法`存在?'为零:NilClass
发表> index.html.erb 在这里:
<h2 class="page-header">Posts <%= link_to "Create New", new_admin_post_path, class:'btn btn-success pull-right' %></h2>
<% if @posts.exists? %>
<table class="table table-striped">
<tr>
<th>Post Title </th>
<th>Date Created</th>
<th>Post Category </th>
<th> Actions </th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.created_at.to_time.strftime('%B %e at %l:%M %p') %></td>
<td><%= post.category.name %></td>
<td><%= link_to "Edit", edit_admin_post_path(post), class:'btn btn-primary' %> <%= link_to "Delete", admin_posts_path(post), class:'btn btn-danger', method: :delete, data: {confirm: 'Are you sure'} %></td>
</tr>
<% end %>
</table>
<%= will_paginate @posts %>
<% else %>
<p> There are no posts </p>
<% end %>
The Posts Controller是:
class PostsController < ApplicationController
def new
@page_title = 'Add Post'
@post = Post.new
end
def create
@post = post.new(post_params)
if @post.save
flash[:notice] = 'Post Created'
redirect_to admin_posts_path
else
render 'new'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
flash[:notice] = 'Post Updated'
redirect_to admin_posts_path
else
render 'new'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
flash[:notice] = 'Post Removed'
end
def index
@posts = Post.all
end
private
def category_params
params.require(:post).permit(:title, :category_id, :user_id, :tags, :image, :body)
end
请给我一个解决方案:(
答案 0 :(得分:0)
像这样检查@post: -
<% if @posts.present? %>
<% end %>
答案 1 :(得分:0)
答案 2 :(得分:0)
你也可以试试这个:
<% if !@posts.nil? %>
<%= your code... %>
<% end %>
这也有助于你的事业。