1

私は ryan bates のプラグイン nested_form を使用しており、 has_many :through 関係のフォームを作成しようとしています。

私は3つのモデルを持っています:

Profile
  has_many :memberships
  has_many :organizations, :through => :memberships
  accepts_attributes_for :organizations
  attr_accessible :organization_attribtues
Membership
  has_many :profiles
  has_many :organizations
Organization
  has_many :memberships
  has_many :profiles, :though => :memberships

以下のフォームはプロファイル用ですが、その中に組織がネストされています。f.fields_for :organizations を実行して組織に関する情報を作成できますが、組織のメンバーシップに固有の情報を更新する方法がわかりません。具体的には、メンバーシップ テーブルに title 属性があります (Organization の未定義メソッド「title」というエラーがスローされるため、以下でコメントアウトしました)。どんな助けでも大歓迎です!ありがとう。

= f.fields_for :organisations do |org|
    = org.input :name, :label => "<strong>Name of the Organization</strong>"
    = org.input :title, :label => "Your role"
    = org.input :description, :as => :text, :label => "Description of the organization",
4

2 に答える 2

2

別の質問でここ StackOverflow で見たように、 hm => t をネストする必要があります

= f.fields_for :memberships do |mem|
= mem.fields_for :organisation do |org|
  .row
    .span5.org_name
      = org.input :name, :label => "<strong>Name of the Organization</strong>"
    .span5
      = mem.input :title, :label => "<strong>Title in the Organization</strong>"
  .row
    .span5
      = mem.input :starting_year, :label => "<strong>Starting Year</strong>"
    .span5
      = mem.input :ending_year, :label => "<strong>Ending Year</strong>"
  .row
    .span10
      = org.text_area :description, :label => "<strong>Description of Organisation</strong>"
= mem.link_to_remove "Remove this oranisation"
= f.link_to_add "Add an organisation", :memberships

しかし、Ryan Bates のプラグインでは、私が知る限りメンバーシップ組織への関連付けが構築されていないため、次のような新しいメソッドを作成しました。

     = f.link_to_add_hmt "Add an organisation", :organisation, :memberships

そして、Ryan Bates のプラグインを逐語的にコピーし、新しいパラメーターを追加して、以下に 2 行を追加しました。

def link_to_add_hmt(*args, &block)
  options = args.extract_options!.symbolize_keys
  association = args.pop
  association_two = args.pop
  options[:class] = [options[:class], "add_nested_fields"].compact.join(" ")
  options["data-association"] = association
  args << (options.delete(:href) || "javascript:void(0)")
  args << options
  @fields ||= {}
  @template.after_nested_form(association) do
    model_object = object.class.reflect_on_association(association).klass.new
    model_object.send(:"build_#{association_two}")
    output = %Q[<div id="#{association}_fields_blueprint" style="display: none">].html_safe
    output << fields_for(association, model_object, :child_index => "new_#{association}", &@fields[association])
    output.safe_concat('</div>')
    output
  end 
  @template.link_to(*args, &block)
end 

「association_two」への参照を探します。これはうまくいきます!

于 2011-11-05T12:38:39.223 に答える
1

nested_form の最新バージョン用に更新

https://gist.github.com/stuartchaney/7274894

于 2013-11-02T02:46:27.010 に答える