如何在jquery escape_javascript中传递form_builder

时间:2013-02-02 06:20:15

标签: ruby-on-rails ruby-on-rails-3 forms actionview actionviewhelper

我有两个类MessageComment,其关联如下

class Message ActiveRecord::Base
  attr_accessible :comments_attributes
  has_many :comments
  accepts_nested_attributes_for :comments
end

class Comment ActiveRecord::Base
  belongs_to :message
end

我将表单建模为

= form_for @message do |f|
  f.text_field :msg
  %a#add-comment Add Comment

_comment.html.haml
  = f.fields_for :comments do |c|
      c.text_field :value

点击“添加评论”按钮后,我将评论输入附加到表单f,通过jquery,如下所示

$('#add-comment').click(function() {
  $('#add-comment').prepend(("#{escape_javascript(render(:partial => "comment", f: f))}");  
});

但我无法访问此表单,我正在

Undefined local variable or method 'f'

怎么做?

1 个答案:

答案 0 :(得分:0)

在表单中尝试此操作。我假设:

  = f.fields_for :comments do |c|
      c.text_field :value

您的评论中有部分内容。

所以你的表单应该是这样的,并将f变量传递给部分

= form_for @message do |f|
  f.text_field :msg
  %a#add-comment Add Comment

  = render :partial => 'comment', :locals => {:f => f}