Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 尝试使用accepts_nested_attributes_for and has_and_attributes_to_many,但未填充联接表_Ruby On Rails_Ruby_Has And Belongs To Many - Fatal编程技术网

Ruby on rails 尝试使用accepts_nested_attributes_for and has_and_attributes_to_many,但未填充联接表

Ruby on rails 尝试使用accepts_nested_attributes_for and has_and_attributes_to_many,但未填充联接表,ruby-on-rails,ruby,has-and-belongs-to-many,Ruby On Rails,Ruby,Has And Belongs To Many,我正在学习RoR并尝试使用accepts_nested_attributes_for and has_and_attributes_to_many来提交传统上有两种形式的信息。我在一些网站上读到过它们是兼容的,有些网站不兼容,有些网站不知道。作为参考,我使用的是Rails 2.3.4。我试着根据Ryan的碎片教程对我的解决方案进行建模 从我试图调试的内容来看,似乎我有两个问题,但我不确定为什么 当我提交带有嵌套模型的表单时,只发布部分嵌套模型信息。我只得到第一个字段,而不是用户可能选择的“n”其

我正在学习RoR并尝试使用accepts_nested_attributes_for and has_and_attributes_to_many来提交传统上有两种形式的信息。我在一些网站上读到过它们是兼容的,有些网站不兼容,有些网站不知道。作为参考,我使用的是Rails 2.3.4。我试着根据Ryan的碎片教程对我的解决方案进行建模

从我试图调试的内容来看,似乎我有两个问题,但我不确定为什么

  • 当我提交带有嵌套模型的表单时,只发布部分嵌套模型信息。我只得到第一个字段,而不是用户可能选择的“n”其他字段
  • 在发布的单个字段中,没有任何行插入到我为HABTM关系创建的联接表中
  • 以下是我插入尝试的一段代码和相应日志:

    律师模式:

    class Attorney < ActiveRecord::Base 
      has_and_belongs_to_many :associations
      accepts_nested_attributes_for :associations, :reject_if => proc { |a| a['name'].blank? }
    end
    
    律师的新观点:

    <% form_for(@attorney, :html => {:multipart => true}) do |f| %>
      <%= f.error_messages %>
     <%= f.label :"First name" %> 
     <%= f.text_field :firstname %><br>
    
     <%= f.label :"Last Name" %> 
     <%= f.text_field :lastname %><br>
    
     <%= f.label :"Attorney Type" %> 
     <%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br>
    
     <%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br>
    
     <%= f.label :"Attorney Location" %> 
     <%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br>
    
     <div id="associations">
          <%= render :partial => 'shared/membership' %>
     </div>
     <%= add_association_link "Add Association" %>
        <%= f.submit 'Create' %>
    <% end %>
    
    联接表迁移:

    class CreateAssociationsAttorneys < ActiveRecord::Migration
      def self.up
        create_table :associations_attorneys do |t|
          t.references :attorney, :null => false
          t.references :association, :null => false
          t.timestamps
        end
      end
    
      def self.down
        drop_table :associations_attorneys
      end
    end
    

    我可以看到关联“=>{”association\u id“=>“3”}它只获取我为特定人员所拥有的多个关联中的最后一个,并且没有在联接表中创建任何条目。我的代码可能哪里出错了?

    你们两个在这里有问题,不幸的是其中一个被另一个掩盖了

    这两个问题都源于这部分观点:

    <div class="association">
      <% fields_for :associations do |assoc_form| %>
        <%= assoc_form.collection_select(:association_id, Association.find(:all),
          :id, :name, :include_blank => true) %>
    
    新律师视图:

    def new
      @attorney = Attorney.new
      @attorney.associations.build
      @attorney.attorney_associations.build
    
    
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @attorney }
      end
    end
    
    <% form_for(@attorney, :html => {:multipart => true}) do |f| %>
      <%= f.error_messages %>
     <%= f.label :"First name" %> 
     <%= f.text_field :firstname %><br>
    
     <%= f.label :"Last Name" %> 
     <%= f.text_field :lastname %><br>
    
     <%= f.label :"Attorney Type" %> 
     <%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br>
    
     <%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br>
    
     <%= f.label :"Attorney Location" %> 
     <%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br>
    
     <div id="associations">
       <% f.fields_for :attorney_association do |aa_form| %>
         <%= render :partial => 'attorney_association', :locals => {:f => aa_form} %>
       <% end %>
       <%= add_association_link "Add Another Existing Association" %>
       <% f.fields_for :associations do |assoc_form| %>
         <%= render :partial => 'attorney', :locals => {:f => assoc_form} %>       
       <%= create_association_link, "Create a New Association for this Attorney" %>
     </div>
    
    
    
     <%= f.submit 'Create' %>
    <% end %>
    
      <div class="attorney_association">
        <%= f.collection_select(:association_id, Association.find(:all),
          :id, :name, :include_blank => true) %>
        <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %>
      </div>
    
      <div class="association">
        <%= f.label_for :name %>
        <%= f.text_field :name %>
        <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %>
      </div>
    
    {:multipart=>true})do | f |%>
    


    70,:行=>20%>


    ‘律师协会’,:locals=>{:f=>aa_form}%> ‘律师’,:locals=>{:f=>assoc_form}%>
    我假设add_association_link是一个javascript帮助程序,它创建一个链接来克隆成员部分的空实例。create_association_link是一个类似帮助程序的占位符,该帮助程序将为新关联添加部分

    律师协会部分:

    def new
      @attorney = Attorney.new
      @attorney.associations.build
      @attorney.attorney_associations.build
    
    
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @attorney }
      end
    end
    
    <% form_for(@attorney, :html => {:multipart => true}) do |f| %>
      <%= f.error_messages %>
     <%= f.label :"First name" %> 
     <%= f.text_field :firstname %><br>
    
     <%= f.label :"Last Name" %> 
     <%= f.text_field :lastname %><br>
    
     <%= f.label :"Attorney Type" %> 
     <%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br>
    
     <%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br>
    
     <%= f.label :"Attorney Location" %> 
     <%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br>
    
     <div id="associations">
       <% f.fields_for :attorney_association do |aa_form| %>
         <%= render :partial => 'attorney_association', :locals => {:f => aa_form} %>
       <% end %>
       <%= add_association_link "Add Another Existing Association" %>
       <% f.fields_for :associations do |assoc_form| %>
         <%= render :partial => 'attorney', :locals => {:f => assoc_form} %>       
       <%= create_association_link, "Create a New Association for this Attorney" %>
     </div>
    
    
    
     <%= f.submit 'Create' %>
    <% end %>
    
      <div class="attorney_association">
        <%= f.collection_select(:association_id, Association.find(:all),
          :id, :name, :include_blank => true) %>
        <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %>
      </div>
    
      <div class="association">
        <%= f.label_for :name %>
        <%= f.text_field :name %>
        <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %>
      </div>
    
    
    正确)%%>
    
    关联部分:

    def new
      @attorney = Attorney.new
      @attorney.associations.build
      @attorney.attorney_associations.build
    
    
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @attorney }
      end
    end
    
    <% form_for(@attorney, :html => {:multipart => true}) do |f| %>
      <%= f.error_messages %>
     <%= f.label :"First name" %> 
     <%= f.text_field :firstname %><br>
    
     <%= f.label :"Last Name" %> 
     <%= f.text_field :lastname %><br>
    
     <%= f.label :"Attorney Type" %> 
     <%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br>
    
     <%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br>
    
     <%= f.label :"Attorney Location" %> 
     <%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br>
    
     <div id="associations">
       <% f.fields_for :attorney_association do |aa_form| %>
         <%= render :partial => 'attorney_association', :locals => {:f => aa_form} %>
       <% end %>
       <%= add_association_link "Add Another Existing Association" %>
       <% f.fields_for :associations do |assoc_form| %>
         <%= render :partial => 'attorney', :locals => {:f => assoc_form} %>       
       <%= create_association_link, "Create a New Association for this Attorney" %>
     </div>
    
    
    
     <%= f.submit 'Create' %>
    <% end %>
    
      <div class="attorney_association">
        <%= f.collection_select(:association_id, Association.find(:all),
          :id, :name, :include_blank => true) %>
        <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %>
      </div>
    
      <div class="association">
        <%= f.label_for :name %>
        <%= f.text_field :name %>
        <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %>
      </div>
    
    
    
    通过将视图更改为以“f”的形式传递,我现在得到了错误:未定义的方法“association_id”for#在这一行的partial:true)%>。有什么想法吗?看起来我忽略了您遇到的许多其他问题。我也更新了解决方案来解决这些问题。所以我已经为此工作了几周,现在仍然很开心正在解决问题。当它试图使律师协会部分化时,它抱怨不知道什么是“f”…我以为表单对象是通过:locals散列传递的?复杂表单示例存储库确实有一个使用嵌套属性的HABTM。但当然,它不起作用。Sh*t。如果实现is案例需要“几个星期”,但我猜“Ruby black magic”是什么。不幸的是,我不得不改变一个现有的项目,而且我每天得到的Rails资金越来越少。模型和视图都要混合在一起。不过,上面的例子很好-谢谢你的帮助!