Spree 3.1:在发货步骤中添加 select 标签到发货表单
Spree 3.1: Adding select tag to shipment form in delivery step
我正尝试在结帐时在发货表格中为城市添加一个 select 标签。
#views/spree/checkout/_delivery.html.erb
<%= form.fields_for :shipments do |ship_form| %>
<% ship_form.object.shipping_rates.each do |rate| %>
<tr class="stock-item">
<td class="shipment-button"><%= ship_form.radio_button :selected_shipping_rate_id, rate.id %></td>
<td class="rate-name"><%= rate.name %></td>
<td class="item-qty"></td>
<td class="rate-cost"><%= rate.display_cost %></td>
</tr>
<% if rate.shipping_method_id == 4 %>
<tr id="city" style="display: none">
<td></td>
<td>
<%= ship_form.select :selected_city_id, options_from_collection_for_select(@cities, "id", "name"), { :include_blank => true }, { :class => "select-city" } %>
</td>
<td></td>
<td></td>
</tr>
<% end %>
<% end %>
型号发货:
#models/spree/shipment.rb
def selected_shipping_rate
shipping_rates.where(selected: true).first
end
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
def selected_city_id
selected_shipping_rate.try(:selected_city_id)
end
结帐控制器:
#controller/spree/checkout_controller.rb
def update
if @order.update_from_params(params, permitted_checkout_attributes, request.headers.env)
@order.temporary_address = !params[:save_user_address]
unless @order.next
flash[:error] = @order.errors.full_messages.join("\n")
redirect_to(checkout_state_path(@order.state)) && return
end
if @order.completed?
@current_order = nil
flash.notice = Spree.t(:order_processed_successfully)
flash['order_completed'] = true
redirect_to completion_route
else
redirect_to checkout_state_path(@order.state)
end
else
render :edit
end
end
Select框用于选择可用送货方式的城市。
我在 table spree_shipments
中添加了新列并且
我还在 models/spree/shippment.rb
中添加了方法 selected_city_id
并允许在初始化程序中使用新参数。
但是当我试图获得 @order.shipments.order("created_at").last.selected_city_id
我得到 NoMethodError: undefined method selected_city_id
.
提交后的 Prams 如下所示:
"order"=>{
"shipments_attributes"=>{
"0"=>{"selected_city_id"=>"10",
"selected_shipping_rate_id"=>"10",
"id"=>"4"}
}
}
我想我在这里遗漏了一些东西...
请有人帮助我,如何在数据库中保存 selected_city_id
?是否应该保存在不同的 table (spree_shipping_rates)?
谢谢
解法:
我在 Shipment 模型中添加了一种方法
#models/spree/shipment.rb
def selected_shipping_rate
shipping_rates.where(selected: true).first
end
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
def selected_city_id
selected_shipping_rate.try(:selected_city_id)
end
def selected_city_id=(id)
selected_shipping_rate.update(selected_city_id: id)
end
我正尝试在结帐时在发货表格中为城市添加一个 select 标签。
#views/spree/checkout/_delivery.html.erb
<%= form.fields_for :shipments do |ship_form| %>
<% ship_form.object.shipping_rates.each do |rate| %>
<tr class="stock-item">
<td class="shipment-button"><%= ship_form.radio_button :selected_shipping_rate_id, rate.id %></td>
<td class="rate-name"><%= rate.name %></td>
<td class="item-qty"></td>
<td class="rate-cost"><%= rate.display_cost %></td>
</tr>
<% if rate.shipping_method_id == 4 %>
<tr id="city" style="display: none">
<td></td>
<td>
<%= ship_form.select :selected_city_id, options_from_collection_for_select(@cities, "id", "name"), { :include_blank => true }, { :class => "select-city" } %>
</td>
<td></td>
<td></td>
</tr>
<% end %>
<% end %>
型号发货:
#models/spree/shipment.rb
def selected_shipping_rate
shipping_rates.where(selected: true).first
end
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
def selected_city_id
selected_shipping_rate.try(:selected_city_id)
end
结帐控制器:
#controller/spree/checkout_controller.rb
def update
if @order.update_from_params(params, permitted_checkout_attributes, request.headers.env)
@order.temporary_address = !params[:save_user_address]
unless @order.next
flash[:error] = @order.errors.full_messages.join("\n")
redirect_to(checkout_state_path(@order.state)) && return
end
if @order.completed?
@current_order = nil
flash.notice = Spree.t(:order_processed_successfully)
flash['order_completed'] = true
redirect_to completion_route
else
redirect_to checkout_state_path(@order.state)
end
else
render :edit
end
end
Select框用于选择可用送货方式的城市。
我在 table spree_shipments
中添加了新列并且
我还在 models/spree/shippment.rb
中添加了方法 selected_city_id
并允许在初始化程序中使用新参数。
但是当我试图获得 @order.shipments.order("created_at").last.selected_city_id
我得到 NoMethodError: undefined method selected_city_id
.
提交后的 Prams 如下所示:
"order"=>{
"shipments_attributes"=>{
"0"=>{"selected_city_id"=>"10",
"selected_shipping_rate_id"=>"10",
"id"=>"4"}
}
}
我想我在这里遗漏了一些东西...
请有人帮助我,如何在数据库中保存 selected_city_id
?是否应该保存在不同的 table (spree_shipping_rates)?
谢谢
解法:
我在 Shipment 模型中添加了一种方法
#models/spree/shipment.rb
def selected_shipping_rate
shipping_rates.where(selected: true).first
end
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
def selected_city_id
selected_shipping_rate.try(:selected_city_id)
end
def selected_city_id=(id)
selected_shipping_rate.update(selected_city_id: id)
end