Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌套的表单,有许多:通过,更新连接模型上的属性_Ruby On Rails_Ruby On Rails 3.1_Nested Forms_Nested Form For - Fatal编程技术网

Ruby on rails 嵌套的表单,有许多:通过,更新连接模型上的属性

Ruby on rails 嵌套的表单,有许多:通过,更新连接模型上的属性,ruby-on-rails,ruby-on-rails-3.1,nested-forms,nested-form-for,Ruby On Rails,Ruby On Rails 3.1,Nested Forms,Nested Form For,我正在使用ryan bates的插件嵌套表单,我一直在尝试为一个“有很多:通过关系”编写表单 我有3种型号: Profile has_many :memberships has_many :organizations, :through => :memberships accepts_attributes_for :organizations attr_accessible :organization_attribtues Membership has_many :pr

我正在使用ryan bates的插件嵌套表单,我一直在尝试为一个“有很多:通过关系”编写表单

我有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属性(我在下面对其进行了注释,因为它为组织抛出了一个未定义的错误方法'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",
=f.fields_代表:组织做|组织|
=org.input:name,:label=>“组织的名称”
=org.input:title,:label=>“您的角色”
=org.input:description,:as=>:text,:label=>“组织描述”,

正如我在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的插件,添加了一个新参数,在下面添加了两行

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 
def link_to_add_hmt(*args,&block)
options=args.extract\u options!。象征着你的钥匙
关联=args.pop
关联_two=args.pop
选项[:类]=[选项[:类],“添加嵌套字段”]。压缩。连接(“”)
选项[“数据关联”]=关联

args更新为最新版本的嵌套表单

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