为什么我会收到“无路线”消息远程发布与远程更新帖子时出错?

发布于 2024-12-10 18:55:46 字数 792 浏览 0 评论 0原文

一些背景:我有一个帖子控制器,用于用户创建或更新帖子。在帖子表单中,用户开始撰写帖子。该帖子可以保存为草稿(然后表单切换功能以提交到控制器的“更新”操作)并稍后发布到页面,或者直接发布到页面。

这些表单远程发布到一个页面,该页面会附加一个包含帖子的表格。

目前,我遇到一个问题:提交表单失败,因为在附加表格时,我收到错误“ActionView::Template::Error (No Route matches {:action=>"vote", :controller=>; “帖子”,:post_id=>245,:积极=>true})”。这是指帖子视图中的一行代码:

 <%= link_to (image_tag("icons/upvote.png", :height => "15px")), vote_post_path(:post_id => post.id, :positive => true), :remote => true %>

我应该提到,将帖子追加到表中是通过使用控制器中仍然可用的 @post 变量来完成的。

现在奇怪的是,只有当帖子直接提交到“创建”操作并尝试将帖子附加到表中时,才会发生此错误。当保存的草稿提交到“更新”操作并附加到表中时,不会发生这种情况。

有谁对这是为什么有任何建议吗?我可以在相关的地方提供更多代码。

编辑:我的路线文件的相关部分:

 resources :posts do
   member do
     get :vote
   end
 end

Some background: I have a Posts controller which is used for users to create or update posts. In a post form, a user starts writing their post. This post can either be saved as a draft (and then the form switches functionality to submit to the "update" action of the controller) and later be posted to a page, or be directly posted to a page.

These forms post remotely to a page, which appends a table with the post inside.

Currently, I have a problem: submitting a form breaks down because, in appending the table, I get an error "ActionView::Template::Error (No route matches {:action=>"vote", :controller=>"posts", :post_id=>245, :positive=>true})". This refers to a line of code in the posts view:

 <%= link_to (image_tag("icons/upvote.png", :height => "15px")), vote_post_path(:post_id => post.id, :positive => true), :remote => true %>

I should mention that appending the post to the table is done by using an @post variable that remains available from the controller.

NOW the weird thing is that this error ONLY occurs when a post has been submitted directly to the "create" action, and trying to append the table with the post. When a saved draft is submitted to the "update" action and appended onto the table, this doesn't occur.

Does anyone have any advice as to why this is? I can provide more code where relevant.

EDIT: the relevant section of my routes file:

 resources :posts do
   member do
     get :vote
   end
 end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

老娘不死你永远是小三 2024-12-17 18:55:46

问题是您将“投票”操作作为成员操作,而不是收集操作。将其切换为:

 resources :posts do

   collection do
      get :vote 

   end
 end

the problem is that you have the "vote" action as a member action, not a collection action. Switch it to:

 resources :posts do

   collection do
      get :vote 

   end
 end
忆梦 2024-12-17 18:55:46

尝试一下,看看会发生什么:

resources :posts do
  resources :votes 
end

Try this and see what happens:

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