チェックアウトの配送で出荷形態の都市用にもう1つの選択タグを追加しようとしています。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
選択ボックスは配送方法が利用可能である都市を選択するためです。
私はまたmodels/spree/shippment.rb
方法selected_city_id
を添加し、初期化中に新しいパラメータを許可テーブルspree_shipments
と に新しい列を追加しました。
しかし、私が取得しようとすると@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 –
おかげで終わらない、方法にいくつかの他の名前を付けてみたがそれはdoesnのt助け... – bonekost
モデルとコントローラコードお願い(必要な部分のみ) –