简体   繁体   中英

Adding id's to comments

Rails newbie here. A seemingly simple issue has me stuck. I am unable to view the comments#show page. I think it is because my comments don't have individual id's. I added comment_id to my comments table with no success. I'm getting the following error:

No route matches missing required keys: [:id]

Here is my comments#index file:

<% @post.comments.each do |comment| %>
<div>
<strong><td><%= link_to (comment.title), post_comment_path(@post) %></strong>
<%= comment.subtitle %>
</div>
<% end %>

Here is my migration for adding a comment_id column:

rails generate migration AddComment_idToComments comment_id:integer

My routes.rb file:

resources :posts do

 resources :comments
 resources :pictures
end

devise_for :users
root to: 'posts#index'
match '/about', to: 'pages#about',  via:'get'

Here is my comments table:

create_table "comments", force: true do |t|
t.integer  "post_id"
t.integer  "user_id"
t.string   "title"
t.string   "subtitle"
t.string   "body"
t.datetime "created_at"
t.datetime "updated_at"
t.integer  "comment_id"
end    

Any help is appreciated!

I'm assuming that you have defined the relationship between a post and a comment such that a post has many comments or a comment belongs to a post. Your comments table needs a field called id and another field called post_id , assuming again that the table that contains the posts is called posts and the model is named Post .

This lets a comment reference the post that it belongs to. For example let's say you had this post:

------------------------
| id | title | content |
| -- | ----- | ------- |
| 24 | Lorem | Ipsum   |
------------------------

Now a comment can attach itself to this post by using the post_id field:

-----------------------------------
| id | post_id | title | subtitle |
| -- | ------- | ----- | -------- |
| 19 | 24      | Lorem | Ipsum    |
-----------------------------------

I would ask why you have a single @post variable in your comments index but assuming that your code is creating a list of links, to make them point to each individual comment's show action I believe you are looking for:

<%= link_to comment.title, comment %>

Also, you have a rogue <td> tag in there that's going to mess up the rendering in the comments index.

In addition, unless you have explicitly set the id to false in the migration when you created the model, it has an id by default that acts as its primary key. If you did set the id to false when you created the model you can add it back by running a migration on the table with

add_column :comments, :id, :primary_key

but assuming you did not set :id => false in your original migration, then it already has an id that you can access with comment.id , this is how the Comment.find(params[:id]) that you most likely in your Comments#show action works.

post_comment_path(:id => comment.id) can you please try this

Thanks

I think problem in your route helper:

post_comment_path(@post) 

must be

post_comment_path(comment)

You can saw do your comment have id column or not in your db/schema.rb file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM