我遇到了我的郵政編碼問題,請看看這個。 錯誤是說Admin中的NoMethodError :: 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 %>
和帖子控制器是:
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
請給我一個解決方案:(
這是行得通的。謝謝, –
但爲什麼我的存在不起作用? –
歡迎.. :) 請參閱此鏈接http://stackoverflow.com/questions/13186722/what-is-the-difference-between-using-exists-and-present-in-ruby – user3506853