路线存在,但无法破坏对象,为什么?

发布于 2025-01-06 16:46:58 字数 1204 浏览 3 评论 0原文

我无法销毁具有有效路线的对象。浏览器返回没有路由匹配[POST]“/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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2025-01-13 16:46:58

我认为你的 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 action destroy, so you won't have @post or @topic for that matter, because :fetch_topic doesn't get called either.

长亭外,古道边 2025-01-13 16:46:58

它应该是method: :delete

It should be method: :delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文