dimanche 28 juin 2015

Undefined Method for `topic_comment_path`

After I make a comment for any particular post and submit the comment via the Create action to add the comment to the database, I get the error Undefined Method for topic_comment_path. From the error, it looks like the route path is not picking up the local variable comment that I am looping through(@post.comments.each do |comment|) in the show.html.erb file? When I refresh the page, and go back and check the post I see the number of comments has increased due to the post being created. But I can't view the comment after I create it on the Posts#show view. Can anyone offer any assistance?

From Show.html.erb file

<% 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 %>

Screenshot of error in browser

topic_post_comment_path error

Comments table schema

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

Post controller detail showing the new and create methods for @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

Comments Controller

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

Rake output

 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

rake routes

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

Aucun commentaire:

Enregistrer un commentaire