0

我有类似于最近Ryan Bates 截屏视频的复杂形式

然而,嵌套元素工作正常。我正在通过以当天价格为输入的表单创建或更新诸如此类的数据网格。当他们留下一个空白时,我的问题就开始了。我有用于不保存 nils 的 nested_attributes_for 选项,它可以工作,如果它们只在特定行中保存一个值,它会保存正确的日期,但是当重新加载时,它会将其放置在错误的列中。我不确定如何将值连续排列到表单中。IE 星期三的保存值将出现在星期一列(正确的行)。如果他们为一行保存所有值,则不会发生这种情况(然后它会完美运行)。

数据像这样存储在数据库中

ID OBJECT_ID DAYOFWEEK PRICE并显示如下

+------+----------------+-------+-------+-------+------+-------+
| id   | name           | Mon   | Tue   | Wed   | Thu  | Fri   | -> +2 more days etc
+------+----------------+-------+-------+-------+------+-------+
| 1234 | Some name      | 87.20 | 87.20 | 87.20 | 82.55| 85.48 |
+------+----------------+-------+-------+-------+------+-------+
| 1234 | Some name      | 87.20 | 87.20 | 87.20 | 82.55| 85.48 |
+------+----------------+-------+-------+-------+------+-------+
| 1234 | Some name      | 87.20 | 87.20 | 87.20 | 82.55| 85.48 |
+------+----------------+-------+-------+-------+------+-------+

构建或显示这些值的控制器代码如下所示:

控制器

@rooms.each do |r|
  ((r.room_rates.size+1)..7).each {
      r.room_rates.build
  }
end

房间.html.erb

<% @dow = 0 %>
  <tr class="room">
 <td><%= f.text_field :name %></td>

 <% f.fields_for :room_rates do |rates| %>
  <%= render 'rates', :f => rates %>

  <% @dow += 1 %>
 <% end %>

 <td class="delete_mode" style="display:none;">
  <%= f.hidden_field :_destroy %>
  <%= link_to_function "remove", "remove_room(this)" %>
 </td>
  </tr>

rate.html.erb

<td> 
 <%= f.text_field :price, :size => 3 %>
 <%= f.hidden_field :dayofweek, :value => @dow %> 
 <%= f.hidden_field :source, :value => 0 %>
</td>

room_rates 模型(表格中的数据要去哪里)

+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| id    | room_id | dayofweek | price | source | created_at                | updated_at                |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| 92745 | 8       | 0         | 1.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92746 | 8       | 1         | 2.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92747 | 8       | 2         | 3.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92748 | 8       | 3         | 4.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92749 | 8       | 4         | 5.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92750 | 8       | 5         | 6.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92751 | 8       | 6         | 7.0   | 0      | 2010-02-23 14:33:05 +0100 | 2010-02-23 14:33:05 +0100 |
| 92752 | 9       | 3         | 5.0   | 0      | 2010-02-23 14:33:33 +0100 | 2010-02-23 14:33:33 +0100 |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+

在控制台中订购

+---------+-----------+-------+--------+---------------------------+---------------------------+
| room_id | dayofweek | price | source | created_at                | updated_at                |
+---------+-----------+-------+--------+---------------------------+---------------------------+
| 2517    | 0         |       |        |                           |                           |
| 2517    | 1         |       |        |                           |                           |
| 2517    | 2         | 3.0   | 0      | 2010-02-23 17:54:28 +0100 | 2010-02-23 17:54:28 +0100 |
| 2517    | 3         | 4.0   | 0      | 2010-02-23 17:54:28 +0100 | 2010-02-23 17:54:28 +0100 |
| 2517    | 4         |       |        |                           |                           |
| 2517    | 5         |       |        |                           |                           |
| 2517    | 6         |       |        |                           |                           |
+---------+-----------+-------+--------+---------------------------+---------------------------+
4

1 回答 1

2

错误是在您创建表单时 - 因为您取决于 room_rates 的顺序是否正确,所以您需要将空(内置)费率放入正确的位置。如果每个房间有很多房价,您需要生成表格,以便房价在一周中的正确日期。此代码将在一个新数组中构建它,并正确设置新数组:

@rooms.each do |r|
  new_rates = []
  (0..6).each { |dow|
    rate = r.room_rates.find_by_dayofweek(dow)
    if rate
      new_rates << rate
    else 
      new_rates << r.room_rates.build(:dayofweek => dow)
    end
  }
  r.room_rates = new_rates
end

或者,如果您指定关联的顺序,您也许可以只构建缺少的周数:

// In room model 
has_many :rates, :order => "dayofweek"

// In controller
@rooms.each do |r|
  (0..6).each { |dow|
    if not r.room_rates.find_by_dayofweek(dow)
      r.room_rates.build(:dayofweek => dow)
    end
  }
end
于 2010-02-23T14:20:30.593 回答