在编辑操作中复制f.fields_for

时间:2014-08-13 10:58:24

标签: ruby-on-rails ruby-on-rails-3

控制器: projects_controller.rb

def new
    @project = Project.new
    @project.repositories.build
end

def edit
  @project = Project.find(params[:id])
end

型号: project_sub_type.rb

class ProjectSubType < ActiveRecord::Base
  has_many :repositories, :dependent => :destroy
  accepts_nested_attributes_for :repositories
end

查看: _form.html.erb

<%= form_for @project, :html => {:class => 'project'} do |f| %>
  <%= f.label :name, "Project name" %>
  <%= f.text_field :name %>
      <%= f.fields_for :repositories do |ff| %>
        <%= ff.check_box :repos_name, {} , "svn_software", nil %> Svn Software
      <% end %>
      <%= f.fields_for :repositories do |ff| %>
        <%= ff.check_box :repos_name, {} , "git_software", nil %> Git Software
      <% end %>
<%= f.submit "Save"%>

edit.html.erb

<h2>Edit project</h2>
<%= render 'form' %>

问题:在创建过程中,会创建如此

的复选框
<input type="checkbox" value="svn_software" name="project[repositories_attributes][0][repos_name]" id="project_repositories_attributes_0_repos_name"> 
<input type="checkbox" value="git_software" name="project[repositories_attributes][1][repos_name]" id="project_repositories_attributes_1_repos_name"> 

它对我来说非常适合。但在编辑期间,它会额外创建2个字段:

<input type="checkbox" value="svn_software" name="project[repositories_attributes][0][repos_name]" id="project_repositories_attributes_0_repos_name"> 
<input type="checkbox" value="svn_software" name="project[repositories_attributes][1][repos_name]" id="project_repositories_attributes_1_repos_name"> 
<input type="checkbox" value="git_software" name="project[repositories_attributes][2][repos_name]" id="project_repositories_attributes_2_repos_name"> 
<input type="checkbox" value="git_software" name="project[repositories_attributes][3][repos_name]" id="project_repositories_attributes_3_repos_name"> 

还会创建一个隐藏字段,如下所示:

<input type="hidden" value="51" name="project[repositories_attributes][0][id]" id="project_repositories_attributes_0_id"> 到.. 3

有人可以指出我的错误。如果我使用1个fields_for并通过数组循环,则存在同样的问题。

任何人都可以帮忙。

提前致谢

1 个答案:

答案 0 :(得分:0)

参考http://apidock.com/rails/v3.2.13/ActionView/Helpers/FormHelper/fields_for

您的代码会导致重复结果,因为The block given to the nested fields_for call will be repeated for each instance in the collection,所以您必须:

<%= form_for @project, :html => {:class => 'project'} do |f| %>
  <%= f.label :name, "Project name" %>
  <%= f.text_field :name %>
    <% @project.repositories.each do |r| %>
      <%= f.fields_for :repositories, r do |ff| %>
        <%= ff.check_box :repos_name, {} , r.repos_name, true %><%=r.repos_name%>
      <% end %>
    <% end %>
<%= f.submit "Save"%>

但是在创建时,您应该填写check_box不包含在存储库中。 所以你可以:

<%= form_for @project, :html => {:class => 'project'} do |f| %>
  <%= f.label :name, "Project name" %>
  <%= f.text_field :name %>
    <% @project.repositories.each do |r| %>
      <%= f.fields_for :repositories, r do |ff| %>
        <%= ff.check_box :repos_name, {} , r.repos_name, true %><%=r.repos_name%>
      <% end %>
    <% end %>
    <% (['svn_software','git_software'] - @project.repositories.map(& :repos_name)).each do |name| %>
      <%= ff.check_box :repos_name, {} , name, nil %><%=name%>
    <% end %>
<%= f.submit "Save"%>