nested_form,has_many:through,更新连接模型上的属性 [英] nested_form, has_many :through, updating attribute on join model

查看:90
本文介绍了nested_form,has_many:through,更新连接模型上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ryan bates的插件nested_form,并且我一直在尝试为has_many:through关系编写表单.

I'm using ryan bates' plugin nested_form and i have been trying to write my form for a has_many :through relationship.

我有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属性(我在下面将其注释掉,因为它会为Organisation抛出一个错误的未定义方法"title").任何帮助将非常感谢!谢谢.

The below form is for the profile but with the organization nested inside of it. I can create information about the organization by doing f.fields_for :organizations, but then I'm unclear on how to update information specific to their membership in the organization. Specifically, there is a title attribute on the membership table ( i commented it out below because it throws an error undefined method `title' for Organisation). Any help would be so very appreciated! Thanks.

= 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",

推荐答案

正如我在StackOverflow上看到的另一个问题一样,我需要像这样嵌套hm => t

As I saw here on StackOverflow with another question, I need to nest the hm => t like so

= 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的插件进行构建,据我所知,与会员组织的关联并未建立,因此我创建了这样的新方法:

BUt with Ryan Bates' plugin, the association to memberships organisations is not built as far as i can tell, so i created a new method like this:

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

然后我几乎只是复制了Ryan Bates的插件,并在下面添加了两行,并添加了一个新参数

And then I pretty much just copied Ryan Bates' plugin verbatim adding a new parameter adding 2 lines below

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"的引用.这很棒!

Look for the references to "association_two". This works great!

这篇关于nested_form,has_many:through,更新连接模型上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆