0

当我单击帖子索引页面上帖子的删除链接时,我不断收到 GETShow操作请求,而不是调用Destroy操作。所以错误读取Unknown action - The action 'show' could not be found for PostsController

我认为问题与我有嵌套路由而不是常规路由这一事实有关。

我查看了 Google 和 StackOverflow 上的许多帖子,但没有一个答案对我有帮助。有什么建议么?

app/views/posts/index.html.erb

<% @posts.each do |post| %>
  <tr>
    <td><%= post.content %></td>
    <td>Username_placeholder</td>
    <td><%= post.created_at %></td>
    <td><%= link_to 'Delete', topic_post_path(@topic, post), confirm: 'Are you sure?', method: :delete %></td>
    <!-- topic_post_path(@topic,post) -->
  </tr>
<% end %>

应用程序/控制器/posts_controller.rb

def index
  @topic = Topic.find(params[:topic_id])
  @forum = Forum.find_by_id(@topic.forum_id)
  @posts = @topic.posts.all
end

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

应用程序/模型/topic.rb

class Topic < ActiveRecord::Base
  has_many :posts, :dependent => :destroy
  belongs_to :forum
  accepts_nested_attributes_for :posts, :allow_destroy => true
  attr_accessible :name, :last_post_id, :posts_attributes
end

应用程序/模型/post.rb

class Post < ActiveRecord::Base
  belongs_to :topic

  attr_accessible :content
end

配置/路由.rb

Minforum::Application.routes.draw do
  root :to => "forums#index"
  resources :forums do
    resources :topics
  end
  resources :topics do 
    resources :posts
  end
end

所有路线

            root        /                                           forums#index
    forum_topics GET    /forums/:forum_id/topics(.:format)          topics#index
                 POST   /forums/:forum_id/topics(.:format)          topics#create
 new_forum_topic GET    /forums/:forum_id/topics/new(.:format)      topics#new
edit_forum_topic GET    /forums/:forum_id/topics/:id/edit(.:format) topics#edit
     forum_topic GET    /forums/:forum_id/topics/:id(.:format)      topics#show
                 PUT    /forums/:forum_id/topics/:id(.:format)      topics#update
                 DELETE /forums/:forum_id/topics/:id(.:format)      topics#destroy
          forums GET    /forums(.:format)                           forums#index
                 POST   /forums(.:format)                           forums#create
       new_forum GET    /forums/new(.:format)                       forums#new
      edit_forum GET    /forums/:id/edit(.:format)                  forums#edit
           forum GET    /forums/:id(.:format)                       forums#show
                 PUT    /forums/:id(.:format)                       forums#update
                 DELETE /forums/:id(.:format)                       forums#destroy
     topic_posts GET    /topics/:topic_id/posts(.:format)           posts#index
                 POST   /topics/:topic_id/posts(.:format)           posts#create
  new_topic_post GET    /topics/:topic_id/posts/new(.:format)       posts#new
 edit_topic_post GET    /topics/:topic_id/posts/:id/edit(.:format)  posts#edit
      topic_post GET    /topics/:topic_id/posts/:id(.:format)       posts#show
                 PUT    /topics/:topic_id/posts/:id(.:format)       posts#update
                 DELETE /topics/:topic_id/posts/:id(.:format)       posts#destroy
          topics GET    /topics(.:format)                           topics#index
                 POST   /topics(.:format)                           topics#create
       new_topic GET    /topics/new(.:format)                       topics#new
      edit_topic GET    /topics/:id/edit(.:format)                  topics#edit
           topic GET    /topics/:id(.:format)                       topics#show
                 PUT    /topics/:id(.:format)                       topics#update
                 DELETE /topics/:id(.:format)                       topics#destroy
4

0 回答 0