Rails 复杂形式和构建订购
我的表单很复杂,类似于最近的 Ryan Bates 截屏视频
然而,嵌套元素工作正常。我正在通过以当天的价格为输入的表单来创建或更新这样的数据网格。当他们留空时我的问题就开始了。我有nested_attributes_for选项,用于不保存nils,它可以工作,如果它们只在特定行中保存一个值,它会保存正确的日期,但是重新加载时,它会将其放置在错误的列中。我不确定如何将行中的值排序到表单中。 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 |
+------+----------------+-------+-------+-------+------+-------+
构建或显示这些值的控制器代码如下所示:
controller
@rooms.each do |r|
((r.room_rates.size+1)..7).each {
r.room_rates.build
}
end
rooms.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>
rates.html.erb
<td>
<%= f.text_field :price, :size => 3 %>
<%= f.hidden_field :dayofweek, :value => @dow %>
<%= f.hidden_field :source, :value => 0 %>
</td>
room_rates model (表单中的数据的去向)
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| 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 | | | | |
+---------+-----------+-------+--------+---------------------------+---------------------------+
I have complex form similar to a recent Ryan Bates screencast
The nested elements work fine however. I'm creating or updating a grid of data such as this through a form where the day's prices are the input. My problem begins when they leave one blank. I have the nested_attributes_for option for not saving nils and it works, if they only save one value in a particular row, it saves the correct day however when reloaded, it will place it in the wrong column. I'm not sure how to order the values in a row to the form. IE A saved value for wednesday will appear in the monday column (of the correct row). This doesn't happen if they save all value for a row (then it works perfectly).
Data is stored in the DB like so
ID OBJECT_ID DAYOFWEEK PRICE
and displaying like below
+------+----------------+-------+-------+-------+------+-------+
| 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 |
+------+----------------+-------+-------+-------+------+-------+
The controller code either building or display these values is like so:
controller
@rooms.each do |r|
((r.room_rates.size+1)..7).each {
r.room_rates.build
}
end
rooms.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>
rates.html.erb
<td>
<%= f.text_field :price, :size => 3 %>
<%= f.hidden_field :dayofweek, :value => @dow %>
<%= f.hidden_field :source, :value => 0 %>
</td>
room_rates model (where the data from the form is going)
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| 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 |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
ordering in the console
+---------+-----------+-------+--------+---------------------------+---------------------------+
| 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 | | | | |
+---------+-----------+-------+--------+---------------------------+---------------------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是在您创建表单时 - 因为您依赖 room_rates 的顺序才能正确,所以您需要将空(建)费率放入正确的位置。如果每个房间有多个房价,您需要生成表格,以便价格显示在一周中的正确日期。此代码将在新数组中构建该数组,并正确设置新数组:
或者,如果您指定关联的顺序,则可以只构建缺失的几周:
The error is when you create the form - because you are depending on the order of the room_rates to be correct, you need to put the empty (built) rates into the correct positions. If each room has many room rates, you need to generate the form so that the rates are at the right day in the week. This code will build that in a new array, and set the new array correctly:
Alternatively, you may be able to just build the missing weeks, if you specify an order for your association: