2012-08-04 22 views
1

我試圖創建一個利用acts_as_commentable_with_threading gem的表單。儘管我有工作代碼,但它有一些糾結。我主要關心的是我必須爲可評論對象的id做一個hidden_​​field。我懷疑有一種方法可以繞過這個,因爲gem的build-in方法。如果你知道如何做到這一點,請分享。目前,這裏是我的代碼是什麼樣子:Rails:實現acts_as_commentable_with_threading表單

在我的印象控制器我有:

@impression = @book.impressions.find_by_user_id(user) 
@new_comment = Comment.build_from(@impression, current_user.id, "") 

在我看來,我有:

<%= form_for @new_comment, :remote => true do |f| %> 
    <%= f.text_area :body %> 
    <%= f.hidden_field :commentable_id, :value => @impression.id %> 
    <%= f.submit 'Submit' %> 
<% end %> 

而在評論控制器我有:

def create 
    @comment = Comment.build_from(Userimpression.find(params[:comment][:commentable_id]), current_user.id, params[:comment][:body]) 
    @comment.save 
end 

在註釋模式:

def self.build_from(obj, user_id, comment) 
    c = self.new 
    c.commentable_id = obj.id 
    c.commentable_type = obj.class.base_class.name 
    c.body = comment 
    c.user_id = user_id 
    c 
end 

回答

4

我知道這是一個古老的答案,但一個很好的教程,涵蓋這種寶石的幾個方面是:http://twocentstudios.com/blog/2012/11/15/simple-ajax-comments-with-rails/

它不會幫助你的孩子處理,但它很容易算出它出。

+0

本指南在show view中使用局部變量'comment'並形成部分,但我得到了「未定義的局部變量或方法」錯誤。我是否應該在控制器中聲明一個局部變量?我不知道如何解決這個問題。 – ahnbizcad 2014-06-03 10:06:34