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 %>
From routes.rb file
Rails.application.routes.draw do
get 'comments/create'
devise_for :users
#the resources method lets you restrict which RESTful routes you want
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
Screenshot of error in browser
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
Aucun commentaire:
Enregistrer un commentaire