Ruby on Rails-为帖子添加评论

时间:2018-05-26 17:16:03

标签: ruby-on-rails

我正在使用ruby on rails创建一个网站,允许用户提交食谱。我称之为Recipeazy。我不确定如何允许用户添加评论(提交评论的用户可以编辑并删除评论)。 这是我的代码的链接:https://ide.c9.io/kingsong/recipeazy 而且我不确定这是否对我有用:https://thinkster.io/tutorials/rails-json-api/adding-comments-to-articles

如果我不清楚,请告诉我是否有任何我应该发布的代码。

感谢。

1 个答案:

答案 0 :(得分:2)

  1. 您提供的两个链接都没有工作。
  2. 你想要完成的事情非常简单!
  3. 这是简单评论设置。其中包含Recipe模型,Comment模型和User模型。所有这些模型都将相互关联。

    <强>模型/ recipe.rb

    class Recipe < ApplicationRecord
      has_many :comments
    end
    

    <强>模型/ user.rb

    class User < ApplicationRecord
      has_many :comments
    end
    

    <强>模型/ comment.rb

    class Comment < ApplicationRecord
      belongs_to :recipe
      belongs_to :user
    end
    

    就像我说的,这是一个简单的评论设置。这不涉及授权或嵌套注释。

相关问题