【问题标题】:Rails: Implementing an acts_as_commentable_with_threading formRails:实现acts_as_commentable_with_threading 表单
【发布时间】:2012-08-04 00:23:06
【问题描述】:

我正在尝试创建一个使用acts_as_commentable_with_threading gem 的表单。虽然我有工作代码,但它有几个问题。我主要担心的是我必须为可评论对象的 ID 创建一个 hidden_​​field。由于 gem 的内置方法 build_from,我怀疑有一种方法可以绕过它。如果你知道怎么做,请分享。目前,我的代码如下所示:

在我的印象控制器中,我有:

@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 %>

在 Comments 控制器中我有:

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

【问题讨论】:

    标签: ruby-on-rails acts-as-commentable


    【解决方案1】:

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

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

    【讨论】:

    • 本指南在显示视图中使用局部变量 comment 并形成局部变量,但我收到“未定义的局部变量或方法”错误。我应该在某个地方的控制器中声明一个局部变量吗?我不确定如何解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2013-11-11
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多