how do i get the comment id
PoVa
I'm developing a simple Blogger app and I'm trying to add commenting functionality to it. I'm trying to add delete/edit functionality to comments, but I don't know how to get the id of the comment I want to edit. This is what the html code looks like:
<div class = "comment">
<p class = "author">Comment by <%= comment.author_name %>
<span class="creationTime"> <%= distance_of_time_in_words(comment.created_at, Time.now) %> ago</span>
</p>
<p class="text"><%= comment.body %></p>
<div class = "Button">
<%= link_to "Edit this comment", edit_article_comment_path(@comment.article_id, @comment.id) %>
</div>
</div>
Article controller display action:
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comment.article_id = @article.id
end
route:
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / articles#index
error i get:
No route matches {:action=>"edit", :article_id=>"1", :controller=>"comments", :id=>nil} missing required keys: [:id]
Sorry if I'm asking a stupid question, I'm new to this
Saha Prosenjit
Your article controller should look like this:
def show
@article = Article.find(params[:id])
@comments = @article.comments
end
Your view should look like this:
<% @comments.each do |comment| %>
<div class = "comment">
<p class = "author">Comment by <%= comment.author_name %>
<span class="creationTime">
<%= distance_of_time_in_words(comment.created_at, Time.now) %> ago
</span>
</p>
<p class="text"><%= comment.body %></p>
<div class = "Button">
<%= link_to "Edit this comment", edit_article_comment_path(@article, comment) %>
</div>
</div>
<% end %>
Also add the association has_many
to the Article model.
has_many :comments
Hope you have been added article_id
to the comment form