在同一页面的Rails中,从数组中向数据库提交多个条目

时间:2014-03-24 18:55:42

标签: ruby-on-rails arrays

我正在尝试从数组中向数据库提交多个条目。因此,我创建了多个新条目并将它们存储在一个数组中。在视图中,我有一个复选框形式,供用户检查他/她想要添加到数据库的那些。单击提交后,我想将所有这些表单添加到数据库中。这是代码:

控制器:

class EventsBoysController < ApplicationController
  def new
    @season = find_season
    @meet = find_meet(@season)
    @athletes_boys = current_coach.athletes.where(boy: true)
    @events_boys = []
    @athletes_boys.each do |athlete|
      @events_boys << @meet.events_boys.new(:athlete_id => athlete.id)
    end
    @events = ["400 IH", "100", "1600", "400", "110 HH", "800", "3200", "200"]
  end

  def create
    debugger
    @season = find_season
    @meet = find_meet(@season)
    @events_boys = @meet.events_boys.create(events_boy_permit)
    # debugger
    if @events_boys.save
      redirect_to @events_boys, notice: 'Season was successfully created.'
    else
      render action: "new"
    end
  end

  private
  def find_season
    Season.find(params[:season_id])
  end

  def find_meet season
    season.meets.find(params[:meet_id])
  end

  def events_boy_permit
    params.require(:events_boy).permit(:season_id, :meet_id, :athlete_id, :boys_400_m_im, :boys_1600_m, :boys_400_m, :boys_110_m_hh, :boys_800_m, :boys_3200_m, :boys_200_m, :boys_100_m, :time_400_m_im, :time_1600_m, :time_400_m, :time_110_m_hh, :time_800_m, :time_3200_m, :time_200_m, :time_100_m, :place_400_m_im, :place_1600_m, :place_400_m, :place_110_m_hh, :place_800_m, :place_3200_m, :place_200_m, :place_100_m)
  end

end

查看:

<h1><%= "Create new events for the boys for #{@meet.name}" %></h1>

<table id="events-table">
  <tr>
    <th></th>
    <% @events.each do |event| %>
      <th><%= event %></th>
    <% end %>
  </tr>

  <% @events_boys.each do |event| %>
    <tr>
      <td><%= Athlete.find_by_id(event.athlete_id).name %></td>
      <%= form_for [@season, @meet, event], :html => { :mulitpart => true } do |f| %>

        <td><%= f.check_box :boys_400_m_im %></td>

        <td><%= f.check_box :boys_100_m %></td>

        <td><%= f.check_box :boys_1600_m  %></td>

        <td><%= f.check_box :boys_400_m  %></td>

        <td><%= f.check_box :boys_110_m_hh %></td>

        <td><%= f.check_box :boys_800_m %></td>

        <td><%= f.check_box :boys_3200_m  %></td>

        <td><%= f.check_box :boys_200_m  %></td>

        <td><%= f.submit "Submit", :class => 'btn' %></td>

      <% end %>
    </tr>
  <% end %>

</table>

<script type="text/javascript">
  var athletes = new Array();
  var arrayInAthletes = new Array();
  element = document.getElementById("events-table");
  var athletesArray = element.children[0].children
  for (i=1; i < athletesArray.length; i++) {
    var rowArray = athletesArray[i].children;
    arrayInAthletes = [];
    arrayInAthletes[0] = rowArray[0].innerText;
    var sum = 0;

    for (j=1; j < rowArray.length; j++) {
      var checkBox = rowArray[j].children[0];
      var checkedValue = $('#'+checkBox.id+':checked').val();
      if (checkedValue === "1") {
        arrayInAthletes[sum+1] = rowArray[j].children[0].id;
        sum += 1;
      }
    }

    athletes[i-1] = arrayInAthletes;

  }
</script>

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的是类似下面的代码(需要根据您的示例进行调整)。

Contact.create([
  {first_name: 'Ryan', last_name: 'Smith', email: 'ryan@smith.com'}, 
  {first_name: 'John', last_name: 'Doe', email: 'john@doe.com'}
])

您需要做的就是将数据放入哈希数组中。每个哈希都是您要保存的对象。这将进行批量插入,这是我认为你想要的。

您需要从params中提取已选中/未选中的值,因为这是传递给控制器​​的位置。循环遍历该哈希并在其上执行您的逻辑。