2014-06-28 86 views
6

我是新來的Rails。我正在構建我的第一個應用 - 簡單的博客。我有User和Post模型,每個用戶可以寫很多帖子。現在我想添加評論模式,其中每個帖子可以有很多評論,並且每個用戶都可以評論任何其他用戶創建的帖子。
在Comment模型我有添加評論到用戶和發表的模型(Ruby on Rails)

id \ body \ user_id \ post_id

列。
型號協會:
user.rb

has_many :posts, dependent: :destroy 
has_many :comments 

post.rb

has_many :comments, dependent: :destroy 
belongs_to :user 

comment.rb

belongs_to :user 
belongs_to :post 

SO 3 H我是否在CommentsController中正確定義了創建動作? 謝謝。

UPDATE:
的routes.rb

resources :posts do 
    resources :comments 
end 

comments_controller.rb

def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.create(comment_params) 
    if @comment.save 
     redirect_to @post 
    else 
     flash.now[:danger] = "error" 
    end 
    end 

結果是

--- !ruby/hash:ActionController::Parameters 
utf8: ✓ 
authenticity_token: rDjSn1FW3lSBlx9o/pf4yoxlg3s74SziayHdi3WAwMs= 
comment: !ruby/hash:ActionController::Parameters 
    body: test 
action: create 
controller: comments 
post_id: '57' 

我們可以看到它不發送USER_ID和作品只有當我刪除e validates :user_id, presence: true來自comment.rb的字符串

有什麼建議嗎?

+0

首先,您並不需要直接將評論與用戶相關聯。您可以通過帖子間接將它們關聯到用戶。然後,評論是綁定到@post ...所以@ post.comments.create(comment_params)... –

+0

@RubyRacer那麼,我該如何定義@post?我試過這樣的'@post = Post.find(params [:id])@comment = @ post.comments.create(comment_params)'但是出現錯誤「找不到沒有ID的帖子」 – Alexander

+0

您應該閱讀有關路由和嵌套資源。做到這一點,玩遊戲(因爲你正在學習),當你不能自己做的時候回到這裏。 –

回答

11

在你的方式,你應該把這個:

def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.create(comment_params) 
    @comment.user_id = current_user.id #or whatever is you session name 
    if @comment.save 
    redirect_to @post 
    else 
    flash.now[:danger] = "error" 
    end 
end 

,你也應該從comment_params強參數刪除USER_ID。 希望這會幫助你。

+0

是的,謝謝,多數民衆贊成在 – Alexander

7

協會

給你這裏發生了什麼的定義,你要記住當你創建一個記錄,你基本上填充數據庫。您的關聯用foreign_keys

定義當您詢問如何「爲UserPost模型添加註釋」 - 底線是您不需要;您添加了評論Comment模型,並可以將其與UserPost關聯:

#app/models/comment.rb 
Class Comment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :post 
end 

這提示Rails的尋找user_idpost_idComment模型默認。

這意味着如果你直接創建一個評論,你可以通過簡單地填充你的願望的foreign_keys其關聯到這些團體(或使用Rails的對象來填充它們)

所以,當你要保存comment,你可以此:

#app/controllers/comments_controller.rb 
Class CommentsController < ApplicationController 
    def create 
     @comment = Comment.new(comment_params) 
    end 

    private 

    def comment_params 
     params.require(:comment).permit(:user_id, :post_id, :etc) 
    end 
end 

相反,您可以通過使用標準的Rails對象處理它(作爲接受的答案已經指定)

+0

明白了,謝謝! – Alexander

0
Class CommentsController < ApplicationController 
    before_action :set_user 
    before_action :set_post 

    def create 
    @comment = @post.comments.create(comment_params) 

    if @comment.save 
     redirect_to @post 
    else 
     flash.now[:danger] = "error" 
    end 
    end 

    private 

    set_post 
     @post = User.posts.find(params[:post_id]) 
    end 

    set_user 
     @user = User.find(params[:user_id]) 
    end 

    comment_params 
    params[:comment].permit() 
    end 
+1

只有代碼和「試試這個」答案通常是不鼓勵的,請添加一些解釋。 – user5226582

+0

用戶has_many的帖子和帖子有很多評論因此,首先你並不需要直接與評論相關聯。你可以使用User has_many:comments,通過::posts。所以@ post.comments.create(comment_params)將工作。 – Snehal

+0

謝謝。你應該把它添加到你的答案 - 這聽起來像有用的信息。 (提示 - 重音重音「'」可以讓你格式化單個代碼部分) – user5226582