Submit
    list as parameter array in Rails 4 form, adding params values to params hash using javascript

后端 未结 2 944
后悔当初
后悔当初 2021-01-07 11:57

I have a Rails 4 form that builds a list of parts using AJAX on the form page. Once the list of parts is built in the

    I want to submit the list as an
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 12:37

    I was overthinking this and found a much easier answer. It has been a long time since I've worked on this so I've had to go back over my work to refresh my memory.

    Basically I had the Javascript that builds inserts the default parts and any parts added as just a list of checkboxes with their boxes already checked. That way if the user decides they don't need one of the default parts, or want to remove a part they can just uncheck the box and it doesn't get submitted with the form. The empty checkbox lists start out in form shown above like this:

         Default Parts:

    Other Parts:


    when the dropdown for the type of service is changed on the form it fires the javascript shown in my question above. This gets the parts list and hands it off to this JS:

    $("#default_parts_list").empty();
    
    $("#default_parts_list").append('<%= escape_javascript(render :partial => 'get_default_parts' ) %>');
    

    which renders the _get_default_parts partial thus:

      <%= collection_check_boxes(:service, :part_ids, @parts_list, :id, :sku_name, {}, {checked: true}) do |b| %>
      <%= b.label { b.check_box + b.object.name + " " + b.object.sku} %>
      <% end %>
    

    this creates html appended to the form like this:

    So they are just plain old checkboxes that end up being submitted with the form and end up in the params hash like this:

    Parameters: {"utf8"=>"✓", "authenticity_token"=>"IHXcV0H8NnySxGIBXi8ZA=", 
      "service"=>{"tool_id"=>"121", "name"=>"refresher", "due_date"=>"2014-12-27", 
      "completed"=>"2014-12-27", "service_type_id"=>"2", 
      "part_ids"=>["14", "24", "10", ""], "note"=>""}, "parts_used"=>"", 
      "commit"=>"Create Service", "tool_id"=>"121"}
    

    When this same form is used to edit and exiting service it has to use a different form rather than the form partial above. The edit form looks like this:

    Editing service for <%=" #{@tool.category.name.singularize} / #{@tool.serial}" %>

    <%= form_for ([@tool, @service]) do |f| %> <% if @service.errors.any? %>

    <%= pluralize(@service.errors.count, "error") %> prohibited this service from being saved:

      <% @service.errors.full_messages.each do |msg| %>
    • <%= msg %>
    • <% end %>
    <% end %>
    <%= f.hidden_field :tool_id , :value=>params[:tool_id] %> <%= f.label :name %>
    <%= f.text_field :name %>


    <%= f.label :due_date %>
    <%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>


    <%= f.label :completed %>
    <%= f.text_field :completed, 'data-behaviour' => 'datepicker' %>

    <%= f.label 'Service Type:' %> <%= f.select :service_type_id, ServiceType.all.collect{|s| [s.name, s.id] }, {include_blank: false} %>

    Parts Used
    <%= text_field_tag :parts_used %>

    Any parts that get unchecked here will be returned to inventory
    Parts Used:
    <%= f.collection_check_boxes :part_ids, @service.parts, :id, :name %>

    Other Parts:


    <%= f.label :note %>
    <%= f.text_area :note %>


    <%= f.submit %>
    <% end %>

    I hope this helps anyone doing something similar. You can view the full source app on github: EquipDB on github.com

提交回复
热议问题