Rails:无法销毁帖子 [英] Rails: unable to destroy post
本文介绍了Rails:无法销毁帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建基于 Ruby on Rails 的论坛应用程序.我在 Post Controller 中销毁操作有问题.我的帖子控制器:
I'm building forum app based on Ruby on Rails. I have problem with destroy action in Post Controller. My Posts Controller:
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :set_topic, only: :create
def new
@post = @topic.posts.new
end
def create
@post = @topic.posts.new(post_params)
@post.user = current_user
if @post.save
flash[:notice] = 'Post created successfully!'
redirect_to(:back)
else
render 'shared/_post_form'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to(:back)
flash[:error] = "Post was destroyed!"
end
private
def set_topic
@topic = Topic.find(params[:topic_id])
end
def post_params
params.require(:post).permit(:content)
end
end
这是我的路线:
resources :contact_forms, only: [:new, :create]
match '/contact', to: 'contact_forms#new', via: 'get'
root 'static_pages#home'
resources :forums, only: [:index, :show] do
resources :topics, except: :index
end
resources :topics, except: :index do
resources :posts
end
devise_for :users
resources :users, only: :show
我转到主题显示操作,我有删除帖子的链接:
I go to topic show action and i have link to delete post:
= link_to "Delete", topic_post_path(post.topic.forum.id, post.topic.id), method: :delete, data: {confirm: "You sure?"}, class: 'label alert
当我点击它时,我有以下错误:
When i click it i have following error:
ActiveRecord::RecordNotFound in PostsController#destroy
Couldn't find Post with id=46
有什么想法吗?
推荐答案
您没有通过 post.id
来销毁链接.试试看:
You are not passing post.id
to destroy link. Try out:
= link_to "Delete", topic_post_path(post.topic.id, post.id), method: :delete, data: {confirm: "You sure?"}, class: 'label alert'
UPD:有一个更短的方法来完成这个:
UPD: there is a shorter way to accomplish this:
= link_to "Delete", [post.topic.id, post.id], method: :delete, data: {confirm: "You sure?"}, class: 'label alert'
这篇关于Rails:无法销毁帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文