0
我試圖在結算交付中爲裝運表單中的城市添加一個更多選擇標籤。Spree 3.1:在交付步驟中將選擇標籤添加到裝運表單
#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
選擇框是選擇城市,運輸方法是可行的。
我在表spree_shipments
和 添加新的列我還添加方法selected_city_id
在models/spree/shippment.rb
和允許在初始化新的參數。
但是,當我試圖獲得@order.shipments.order("created_at").last.selected_city_id
我得到NoMethodError: undefined method selected_city_id
。
童車後提交這個樣子的:
"order"=>{
"shipments_attributes"=>{
"0"=>{"selected_city_id"=>"10",
"selected_shipping_rate_id"=>"10",
"id"=>"4"}
}
}
我覺得我缺少的東西在這裏... 請有人可以幫助我,如何保存在數據庫selected_city_id
?是否應將其保存在不同的表格中(spree_shipping_rates)?
感謝
嘗試給方法的一些其他的名字,不能與_id –
感謝意見結束,但它不」 t幫助... – bonekost
模型和控制器代碼請(只有部分是必需的) –