路线存在,但无法破坏对象,为什么?
我无法销毁具有有效路线的对象。浏览器返回没有路由匹配[POST]“/blog/topics/3/posts/1”
。不过,我可以对同一资源执行所有其他操作。如果我可以从控制台创建和销毁对象,我的控制器、模板应该是什么样子?
节省时间 - 这些路由在我当前的配置下也不起作用:
- link_to([@topic, @post]) 返回
- link_to([@topic]) 返回 http://localhost:3000/blog/topics/3 /posts/1。 #仍然不会删除
这是我的控制器:
class Blog::PostsController < ApplicationController
before_filter :fetch_topic, except: [:index]
before_filter :fetch_post, except: [:create, :new]
#stuff that works.
..
..
..
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to blog_topic_posts_url, notice: 'Post deleted.'}
end
#DOES NOT work: redirect_to root_url([:blog, @topic, @post]), notice: 'Post deleted.'
end
private
def fetch_post
@post = @topic.posts.find(params[:id])
end
def fetch_topic
@topic = Topic.find(params[:topic_id])
end
这是我的模板:
<%= link_to 'Destroy', blog_topic_post_path(@topic, @post), method: :destroy, confirm: 'You Sure About This?' %>
I am unable to destroy an object with a valid route. Browser returns No route matches [POST] "/blog/topics/3/posts/1"
. However I can perform all other actions on the same resource. How should my controller, template look given I can create and destroy the object from the console?
Save time-these routes don't work either under my current config:
- link_to([@topic, @post]) returns http://localhost:3000/blog/topics/3/1/posts/1.
- link_to([@topic]) returns http://localhost:3000/blog/topics/3/posts/1. #Still does not delete
Here is my controller:
class Blog::PostsController < ApplicationController
before_filter :fetch_topic, except: [:index]
before_filter :fetch_post, except: [:create, :new]
#stuff that works.
..
..
..
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to blog_topic_posts_url, notice: 'Post deleted.'}
end
#DOES NOT work: redirect_to root_url([:blog, @topic, @post]), notice: 'Post deleted.'
end
private
def fetch_post
@post = @topic.posts.find(params[:id])
end
def fetch_topic
@topic = Topic.find(params[:topic_id])
end
Here is my template:
<%= link_to 'Destroy', blog_topic_post_path(@topic, @post), method: :destroy, confirm: 'You Sure About This?' %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的 link_to 可能是错误的。查看 RoR API link_to 选项应该就像
:method =>; :删除,:确认=> “你确定吗?”
。此外,您的
:fetch_post
过滤器不会针对操作destroy
运行,因此您不会有@post
或@topic< /code> 就此而言,因为
:fetch_topic
也不会被调用。I think your link_to might be wrong. Looking on the RoR API the link_to options should be like
:method => :delete, :confirm => "Are you sure?"
.Also, your
:fetch_post
filter doesn't run for actiondestroy
, so you won't have@post
or@topic
for that matter, because:fetch_topic
doesn't get called either.它应该是
method: :delete
。It should be
method: :delete
.