嵌套的params没有提交表格

时间:2014-02-04 21:31:26

标签: ruby-on-rails forms nested-attributes

我有一个基于此rails cast中的示例的嵌套表单:

http://railscasts.com/episodes/196-nested-model-form-revised

对于我的应用程序,我试图让人们以嵌套的形式上传多个文件。每个步骤都可以有许多设计文件,步骤接受设计文件的嵌套属性:

Step.rb:

class Step < ActiveRecord::Base
   attr_accessible :design_files_attributes
   has_many :design_files
   accepts_nested_attributes_for :design_files, :allow_destroy => :true
   ...
end

DesignFile.rb:

class DesignFile < ActiveRecord::Base
    belongs_to :step
    ...
end

当我尝试在添加了另一个文件后添加其他文件时,我遇到了问题。这就是html中表单的样子:

    <div class="upload_new_files">                          

       <fieldset class="design_file_upload_fieldset">   
            <label for="step_design_files_attributes_0_design_file_path">test.pdf</label>

            <input id="step_design_files_attributes_0__destroy" name="step[design_files_attributes][0][_destroy]" type="hidden" value="false">

            <a href="#" class="remove_fields"><i class="icon-trash"></i> Remove File</a>

            <input id="step_design_files_attributes_0_step_id" name="step[design_files_attributes][0][step_id]" type="hidden" value="451">

            <input id="step_design_files_attributes_0_project_id" name="step[design_files_attributes][0][project_id]" type="hidden" value="120">

            <input id="step_design_files_attributes_0_user_id" name="step[design_files_attributes][0][user_id]" type="hidden" value="15">
            <hr>
      </fieldset>       
      <fieldset class="design_file_upload_fieldset">   
            <input id="step_design_files_attributes_70265135732560_design_file_path" name="step[design_files_attributes][70265135732560][design_file_path]" onchange="javascript: addField(this);" type="file" style="display: none;">      

            <input id="step_design_files_attributes_70265135732560__destroy" name="step[design_files_attributes][70265135732560][_destroy]" type="hidden" value="false">        
            <a href="#" class="remove_fields" style="display: block;"><i class="icon-trash"></i> Remove File</a>    
            <input id="step_design_files_attributes_70265135732560_step_id" name="step[design_files_attributes][70265135732560][step_id]" type="hidden" value="446">        

            <input id="step_design_files_attributes_70265135732560_project_id" name="step[design_files_attributes][70265135732560][project_id]" type="hidden" value="120">      

            <input id="step_design_files_attributes_70265135732560_user_id" name="step[design_files_attributes][70265135732560][user_id]" type="hidden" value="15">     

           <hr> 
     </fieldset>    
    </div>

但是当我提交表单时,design_files_attributes不在参数中。我期待这样的事情:

  Parameters: {"step"=>{ "design_files_attributes"=>{"0"=>{"id"=>"17" ...}}, "70265135732560"=>{...}...}

但是,相反,我得到了这个:

Parameters: {"step"=> {"design_files_attributes"=>{"0"=>{"id"=>"17" ...}}

什么不是我在表单参数中提交的新条目?

修改 这是提交给控制器的完整参数;我之前没有把它包括在内,因为它包含了很多其他的东西,但是如果它有用的话我会在这里:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"...=", "step"=>{"name"=>"New Design File", "description"=>"", "question_attributes"=>{"description"=>"", "decision_attributes"=>{"description"=>"", "question_id"=>""}}, "design_files_attributes"=>{"0"=>{"_destroy"=>"false", "step_id"=>"451", "project_id"=>"120", "user_id"=>"15", "id"=>"17"}}, "last"=>"0", "published_on_date"=>"02/04/2014", "published_on_time"=>"05:02 PM", "timeZone"=>"EST"}, "commit"=>"Update Step", "project_id"=>"120", "id"=>"3"}

_step_form.html.erb

 <%= semantic_form_for [@project, @step], :html => {:multipart => true} do |f| %>

 <%= f.fields_for :design_files, multipart: true do |design_file_form| %>
                  <%= render 'design_file_fields', f: design_file_form %>
              <% end %>
 <p class="add_design_file" style="display:none;"><%= link_to_add_fields raw("<icon class='icon-plus-sign' style='color:green;'></icon> Add Another Design File"), f, :design_files %></p>
<% end %>

_design_file_fields.html.erb

<fieldset class="design_file_upload_fieldset">   
    <% if f.object.new_record? %>
        <%= f.file_field :design_file_path, :onChange=>"javascript: addField(this);" %>
    <% else %>
        <%= f.label :design_file_path, f.object.design_file_path.to_s.split('/').pop() %>
    <% end %>
    <%= f.hidden_field :_destroy %>
    <%= link_to raw('<i class="icon-trash"></i> Remove File'), "#", class: "remove_fields" %>
    <%= f.hidden_field :step_id, :value => @step.id %>
    <%= f.hidden_field :project_id, :value => @project.id %>
    <%= f.hidden_field :user_id, :value => current_user.id %>
    <hr>
</fieldset>

steps.js

  // add a new design file field to the form
  function addField(input) {
    console.log('add design field input');
    // remove the filefield of recently uploaded file and replace with existing file styling
    var filename = $(input).val().split('\\').pop();  
    $(input).parent('fieldset').prepend('<label>'+filename+'</label>');
    $(input).hide();
    $('.add_fields').click();
  }

application_helper.rb

def link_to_add_fields(name, f, association)
  new_object = f.object.send(association).klass.new
  id = new_object.object_id
  fields = f.fields_for(association, new_object, child_index: id) do |builder|
    render(association.to_s.singularize + "_fields", f: builder)
  end
  link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end

1 个答案:

答案 0 :(得分:0)

哇,答案最终与我发布的内容完全无关!问题在于表单本身,它有一个额外的&lt;%end%&gt;元素,导致设计文件fields_for未被合并到表单中。我会留下这个回复,以防它对其他人有用。