0

私は2つのクラスを持っており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

「コメントを追加」ボタンをクリックすると、次のように jquery を使用して、コメント入力をフォーム f に追加します。

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

しかし、私はこのフォームにアクセスできません。

Undefined local variable or method 'f'

これを行う方法?

4

1 に答える 1

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}
于 2013-02-02T09:54:51.627 に答える