Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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 RubyonRails部分标识符中的新对象在1处停止_Ruby On Rails_Partial - Fatal编程技术网

Ruby on rails RubyonRails部分标识符中的新对象在1处停止

Ruby on rails RubyonRails部分标识符中的新对象在1处停止,ruby-on-rails,partial,Ruby On Rails,Partial,我有文章,那有很多文章资产。相当简单。在文章的编辑表单上,我只想添加新的文章资产。我不需要编辑当前的部分,因此我创建了一个类似以下内容的部分: <% f.fields_for :article_assets, article_asset do |builder| -%> <div class="article_asset"> <%= builder.file_field :image %> <%= builder.

我有文章,那有很多文章资产。相当简单。在文章的编辑表单上,我只想添加新的文章资产。我不需要编辑当前的部分,因此我创建了一个类似以下内容的部分:

<% f.fields_for :article_assets, article_asset do |builder| -%>
    <div class="article_asset">
        <%= builder.file_field :image %>
        <%= builder.check_box :is_flagged, :class => "isFlagged" %> isFlagged
    </div>
<% end -%>
点击此链接,在创建的字段下会出现一个新字段,如预期的那样,该字段的名称形式为文章[article_assets_attributes][1][is_flagged]的复选框字段。递增,太完美了!但是,添加另一个具有相同链接的表单也会给出标识符为1的相同表单,即duplicate,这使得提交表单时只有2项而不是3项。有人知道为什么会发生这种情况吗?我能做些什么来解决它


RubyonRails 2.3.11嵌套表单2.3失败。这是一段时间以来我生存的祸根,即使我看过《铁路史》等等。以下是我的方法:

1这在article.rb中出现

    after_update :save_article_assets

    def new_article_asset_attributes=(article_asset_attributes)
      article_asset_attributes.each do |attributes|
        article_assets.build(attributes)
      end
    end

    def existing_article_asset_attributes=(article_asset_attributes)
      article_assets.reject(&:new_record?).each do |article_asset|
        attributes = article_asset_attributes[article_asset.id.to_s]
        if attributes
          article_asset.attributes = attributes
        else
          article_assets.delete(article_asset)
        end
      end
    end

    def save_article_assets
      article_assets.each do |article_asset|
        article_asset.save(false)
      end
    end
2在某个地方的助手中:

def add_article_asset_link(name)
  button_to_function name, :class => "new_green_btn" do |page|
        page.insert_html :bottom, :article_assets, :partial => "article_asset", :object => ArticleAsset.new()
    end
end

def fields_for_article_asset(article_asset, &block)
  prefix = article_asset.new_record? ? 'new' : 'existing'
  fields_for("article[#{prefix}_article_asset_attributes][]", article_asset, &block)
end
3在你的部分:

<% fields_for_article_asset(article_asset) do |aa| %>
    <tr class="article_asset">
      <td><%= aa.text_field :foo %></td>
        <td><%= link_to_function "remove", "$(this).up('.article_asset').remove()" %></td>
    </tr>
<% end %>
4以_形式:

<table>
    <%= render :partial => "article_asset", :collection => @article.article_assets %>
</table>

<%= add_article_asset_link "Add asset" %>
<% fields_for_article_asset(article_asset) do |aa| %>
    <tr class="article_asset">
      <td><%= aa.text_field :foo %></td>
        <td><%= link_to_function "remove", "$(this).up('.article_asset').remove()" %></td>
    </tr>
<% end %>
<table>
    <%= render :partial => "article_asset", :collection => @article.article_assets %>
</table>

<%= add_article_asset_link "Add asset" %>