[英]How do I get id of comment
I'm developing a simple blogger app and I'm trying to add comment functionality to it. 我正在开发一个简单的Blogger应用,并且试图向其添加评论功能。 I'm trying to add delete/edit functionality to the comment, but I don't know how to get id of the comment I'm trying to edit. 我正在尝试为评论添加删除/编辑功能,但是我不知道如何获取我要编辑的评论的ID。 Here is what html code looks like: 这是html代码的样子:
<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 show action: 文章控制者显示动作:
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comment.article_id = @article.id
end
Routes: 路线:
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 very new to this 抱歉,如果我问一个愚蠢的问题,我对此很陌生
Try this ...... 尝试这个 ......
<div class = "Button">
<%= link_to "Edit this comment", edit_article_comment_path(@article.id, @comment.id) %>
</div>
In place of 代替
<div class = "Button">
<%= link_to "Edit this comment", edit_article_comment_path(@comment.article_id, @comment.id) %>
</div>
Hope this will work for you. 希望这对您有用。
You article controller should be look like: 您的文章控制器应如下所示:
def show
@article = Article.find(params[:id])
@comments = @article.comments
end
Your view should be look like: 您的视图应如下所示:
<% @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 %>
Add has_many
association to Article model as well. 也将has_many
关联添加到Article模型。
has_many :comments
Hope you have added article_id
to comments table 希望您已将article_id
添加到评论表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.