使用选择框nested_form gem编辑模型 [英] edit model using selectbox nested_form gem
问题描述
我遇到了一个嵌套的表格gem问题,几天后都无法解决.
I have a nested form gem issue and can't figure it out for days.
在编辑"模型时,为什么我的选择框未填充数据库中的当前值?
When "edit" model, why my selectbox not filled with current value from database?
我的自定义"视图:
<%= nested_form_for @order_detail, :url => create_customize_cart_path do |f| %>
# some field here
<%= f.fields_for :order_customs do |builder| %>
<%= render "order_customs_form", :f => builder %>
<% end %>
<%= f.link_to_add "Add Order Customize", :order_customs %>
<%= f.submit %>
<%end%>
我的局部视图(嵌套):
<%= f.label :pressed_position, "Position" %>
<%= f.select :pressed_position, options_for_select(PRESSED_POSITION), {:include_blank => '-- Select Position --'} %>
<%= f.link_to_remove "Remove Customize" %>
已按固定位置 和数据存储的字符串"值
PRESSED POSITION as CONSTANT and data store "string" value
PRESSED_POSITION = [
["Top", "top"],
["Center","center"],
["Bottom","bottom"],
["Right", "right"],
["Left", "left"],
["Top Left", "top left"],
["Top Center", "top center"],
["Top Right", "top right"],
["Center Left", "center left"],
["Center Center", "center center"],
["Center Right", "center right"],
["Bottom Left", "bottom left"],
["Bottom Center", "bottom center"],
["Bottom Right", "bottom right"]
]
对于文本字段,它可以工作(用当前数据填充),但是如果我使用选择框,则不行
For textfield it works (filled with current data), but if i using selectbox it doesn't
在我的控制器中:
def customize
@order_detail = OrderDetail.find_by_id(decrypting_id(params[:id])) rescue nil
if @order_detail.present?
if (current_user == @order_detail.order.user || temporary_user == @order_detail.order.temp_user_id) && @order_detail.order.order_status_id == 1
1.times{@order_detail.order_customs}
else
# else going here
end
else
# else going here
end
end
你们能告诉我该怎么做才能解决这个问题? 真的很感谢,谢谢
Can You guys tell me what should I do to solve this problem? Realy appreciate it, thank you
推荐答案
我目前无法对其进行测试,但是我会这样做:
I can't test it right now, but I would do it like this:
您的视图 已编辑
<%= nested_form_for @order_detail, :url => create_customize_cart_path do |f| %>
# some field here
<% @order_detail.order_customs.each do |order_custom| %>
<%= f.fields_for :order_customs, order_custom do |builder| %>
<%= render "order_customs_form", :f => builder, :order_custom => order_custom %>
<% end %>
<% end %>
<%= f.link_to_add "Add Order Customize", :order_customs %>
<%= f.submit %>
<%end%>
我们必须将order_custom
对象传递给局部对象
We have to pass the order_custom
object to the partial
部分
<%= f.label :pressed_position, "Position" %>
<%= f.select :pressed_position, options_for_select(PRESSED_POSITION, PRESSED_POSITION.index{|element| element.last==order_custom.pressed_position), {:include_blank => '-- Select Position --'} %>
<%= f.link_to_remove "Remove Customize" %>
此行
PRESSED_POSITION.index{|element| element.last==order_custom.pressed_position)
将返回PRESSED_POSITION
数组的索引,其中最后一个元素等于对象order_custom
的pressed_position
属性.
will return the index of the PRESSED_POSITION
array where the last element is equal to the pressed_position
attribute of the object order_custom
.
我希望它会有所帮助:)
I hope it helps :)
这篇关于使用选择框nested_form gem编辑模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!