Rails - 找不到命名空间参数ID

时间:2016-02-24 22:22:58

标签: ruby-on-rails

我在名为' contents'的文件夹中设置了帖子。当我试图删除记录时,它就崩溃了。为什么我收到此错误?

没有路由匹配{:action =>"编辑",:controller =>"内容/故事",:id => nil}缺少必需的密钥:[: ID]

  

的routes.rb

 namespace :content do
    resources :posts
  end
  

PostsController

def index
  @posts = Post.all
end

def new
  @post = Post.new
end

def create
  @post = current_user.posts.new(post_params)
  if @post.save
    redirect_to content_posts_path
  else
    redirect_to root_path, notice: @post.errors.full_messages.first
  end
end

def show
end

def delete
  @post = Post.find(params[:id])
end

def destroy
  post = Post.find(params[:id]).destroy
  redirect_to :back
end

private
  def post_params
    params.require(:post).permit(:content)
  end
end
  

show.html

<ol>
  <% for p in @posts %>
    <li>
      <%= p.title %>
      <%= link_to 'Edit', edit_content_post_path(@post) %>
      <%= link_to 'Delete', content_post_path(@post), method: :delete %>
    </li>
  <% end %>
</ol>

1 个答案:

答案 0 :(得分:1)

@post未定义,您可能需要使用p代替@post

同样在与show.html行动相对应的show中,不确定如何获得@posts