代码之家  ›  专栏  ›  技术社区  ›  nullnullnull

Rails:实现acts_as_commentable_with_threading表单

  •  1
  • nullnullnull  · 技术社区  · 14 年前

    我正在尝试创建一个使用acts_as_commentable_with_threadinggem的表单。虽然我有工作代码,但它有几个问题。我主要担心的是,我不得不为可注释对象的id创建一个hidden_field。由于gem的内置方法build_from,我怀疑有一种方法可以绕过它。如果你知道如何做到这一点,请分享。目前,我的代码如下所示:

    在我的Impression控制器中,我有:

    @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
    
    1 回复  |  直到 14 年前
        1
  •  4
  •   JuanBoca    13 年前

    我知道这是一个古老的答案,但一个涵盖了这个宝石的几个方面的好教程是: http://twocentstudios.com/blog/2012/11/15/simple-ajax-comments-with-rails/

    这对孩子们的处理没有帮助,但很容易弄清楚。

    推荐文章