`topic_comment_path`

时间:2015-06-27 18:56:44

标签: ruby ruby-on-rails-4

在我对任何特定帖子发表评论并通过“创建”操作提交评论以将评论添加到数据库后,我收到错误Undefined Method for topic_comment_path。从错误中,看起来路径路径没有拾取我在show.html.erb文件中循环(@post.comments.each do |comment|)的局部变量注释?当我刷新页面,然后返回并检查帖子时,我看到由于创建帖子而增加了评论数量。但是我在帖子#show视图中创建评论后我无法查看评论。有人可以提供任何帮助吗?

来自Show.html.erb文件

<% if @post.comments.present? %> 

<h1>Comments</h1>
<% @post.comments.each do |comment| %>
  <div class="media">
    <div class="media-left">
    <%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.avatar? %>
  </div>
<div class="media-body">
    <small>
      <%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
      <% if policy(comment).destroy? %>
        | <%= link_to "Delete", [@topic, @post, comment], method: :delete %>
      <% end %>
    </small>
    <p><%= comment.body %></p>
</div>
  </div>

<% end %>
  

浏览器中的错误屏幕截图

topic_post_comment_path error

评论表架构

create_table "comments", force: :cascade do |t|
t.text     "body"
t.integer  "post_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer  "user_id"
end

显示@comment的新建和创建方法的控制器详细信息:

class PostsController < ApplicationController


 def show
 @post = Post.find(params[:id])
 @topic = Topic.find(params[:topic_id])

 end

 def new
 @topic = Topic.find(params[:topic_id])
 @post = Post.new
 @comment = Comment.new
 authorize @post 
 authorize @comment 
 end


 def create

 @topic = Topic.find(params[:topic_id])

 @post = current_user.posts.build(post_params)
 @post.topic = @topic
 @comment = Comment.find(params[:id])
 authorize @post 
 authorize @comment

  if @post.save
    flash[:notice] = "Your new post was created and saved."
    redirect_to [@topic, @post] #takes you to the new post you created
  else
    flash[:error] = "There was an error saving the post. Please try again."
    render :new # it grabs the new.html.erb file and pastes it in the view
  end
  end


  def edit
  @topic = Topic.find(params[:topic_id])
  @post = Post.find(params[:id])
  @comment = Comment.find(params[:id])
  authorize @post
  end

评论控制器

class CommentsController < ApplicationController
 def create
 @topic = Topic.find(params[:topic_id])
 @post = @topic.posts.find(params[:post_id])
 @comment = @post.comments.new(params.require(:comment).permit(:body))
 @comment.user = current_user
 authorize @comment

  @comment.save!#save the code down in the database

 redirect_to [@topic, @post]
 end

def destroy
 @topic = Topic.find(params[:topic_id])
 @post = @topic.posts.find(params[:post_id])
 @comment = @post.comments.find(params[:id])

 authorize @comment
 if @comment.destroy?
  flash[:notice] = "Comment was removed."
  redirect_to [@topic, @post]
 else
  flash[:error] = "Comment couldn't be deleted. Try again."
  redirect_to [@topic, @post]
end
end
end

佣金输出

 topic_post_summaries POST        /topics/:topic_id/posts/:post_id/summaries(.:format) summaries#create
                     GET         /topics/:topic_id/posts/:post_id/summaries(.:format) summaries#show
  topic_post_comments POST   /topics/:topic_id/posts/:post_id/comments(.:format)  comments#create
                     DELETE /topics/:topic_id/posts/:post_id/comments(.:format)  comments#destroy
         topic_posts 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
                     PATCH  /topics/:topic_id/posts/:id(.:format)                posts#update
                     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
                     PATCH  /topics/:id(.:format)                                topics#update
                     PUT    /topics/:id(.:format)                                topics#update
                     DELETE /topics/:id(.:format)                                topics#destroy

佣金路线

Rails.application.routes.draw do


get 'comments/create'

devise_for :users

  resources :users, only: [:update] #creates new action users#update
  resources :topics do
  resources :posts, except: [:index] do
  resource :summaries, only: [:create, :show]
  resource :comments, only: [:create, :destroy]
  end

 end

  get 'about' => 'welcome#about'

  root to: 'welcome#index'
  end

1 个答案:

答案 0 :(得分:1)

您正在使用resource :comments,这会创建一个独特的资源。当您不需要ID来查找时,奇异的资源非常有用。

由于您要删除特定评论,因此应使用resources :comments