[英]how to pass form_builder in jquery escape_javascript
I have two classes Message
and Comment
which is associated as follows 我有两个类, Message
和Comment
,其关联如下
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
I modeled my form as 我将表格建模为
= 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
When 'Add comment' button is clicked I am appending the comment input to the form f, through jquery as follows 当单击“添加评论”按钮时,我通过jquery将评论输入附加到表单f上,如下所示
$('#add-comment').click(function() {
$('#add-comment').prepend(("#{escape_javascript(render(:partial => "comment", f: f))}");
});
But I can't able to access this form, I am getting 但是我无法访问此表格,我得到
Undefined local variable or method 'f'
How to do this? 这个怎么做?
Try this in your form. 以您的形式尝试。 I assume: 我假设:
= f.fields_for :comments do |c|
c.text_field :value
Is in your comment partial. 在您的评论中有些偏颇。
So your form should be like this and passing the f variable to the partial 所以你的形式应该是这样,并将f变量传递给局部变量
= form_for @message do |f|
f.text_field :msg
%a#add-comment Add Comment
= render :partial => 'comment', :locals => {:f => f}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.