I have two classes Message
and Comment
which is associated as follows
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
$('#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
= form_for @message do |f|
f.text_field :msg
%a#add-comment Add Comment
= render :partial => 'comment', :locals => {:f => f}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.