目前我喜欢这张桌子:
Like Load (0.4ms) SELECT "likes".* FROM "likes"
+----+------------+-------------------------+-------------------------+---------+
| id | article_id | created_at | updated_at | user_id |
+----+------------+-------------------------+-------------------------+---------+
| 1 | 1 | 2014-03-24 19:07:59 UTC | 2014-03-24 19:07:59 UTC | |
| 2 | 2 | 2014-03-24 19:18:59 UTC | 2014-03-24 19:18:59 UTC | |
| 3 | 4 | 2014-03-24 19:19:14 UTC | 2014-03-24 19:19:14 UTC | |
| 4 | 9 | 2014-03-24 19:47:36 UTC | 2014-03-24 19:47:36 UTC | |
+----+------------+-------------------------+-------------------------+---------+
4 rows in set
正如您所看到的,当他们喜欢文章时,它不会填写user_id信息。
如何填写上表中的信息?
这是ArticleController中的类似方法:
def like_vote
@article = Article.find(params[:id])
@article.likes.create
redirect_to(article_path)
end
我很想弄清楚如何在上述方法中插入user_id,所以Like表存储了他们的user_id。
帮助?
如果需要:这是视图
<td><%= pluralize(article.likes.count, "like") %></td>
<td><%= button_to '+1', "/articles/#{article.id}/user/#{current_user.id}/like_vote", method: :post %></td>
答案 0 :(得分:0)
您应该设置user_id
,例如:
@article.likes.create(user_id: current_user.id)