Reputation: 283
There are tables of users. And users have to mark out those comments of other users which answer is pleasant to them. How to make "likes" to comments (responses)?
Models.rb
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end
Controller.rb
class CommentsController < ApplicationController
def index
@comments = Comment.all
@users = User.all
end
end
View
<h1>Listing comments</h1>
<table>
<thead>
<tr>
<th>Text</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<% @users.each do |user| %>
<tr>
<td><%= comment.text %></td>
<td><%= comment.user.name %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Comment', new_comment_path %>
Upvotes: 0
Views: 48
Reputation: 2796
You will probably need an another model - Like
.
class Comment < ActiveRecord::Base
belongs_to :user
has_many :likes
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :commend
end
Controller:
def like
Like. find_or_create_by_user_id_and_comment_id(current_user.id, params[:commend_id])
end
Something like this. You'll also have to take care of validation and so on.
Upvotes: 1