我在使用rails select标签时遇到问题。在这两种情况下,无论选择什么,select_tag都会传递数组中的第一个项目。
user_address数组来自我的一个模型,交付计划只有以下两个选项。我一直在阅读文档,但每当我尝试添加selected => 'plan'
时,我总会收到错误。
有人可以就正确的语法提出建议
<<%= form_tag charges_path, id: 'chargeForm' do %>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail', current_user.email %>
<%= hidden_field_tag 'amount', final_charge %>
<%= text_field_tag 'tax', tax_amount %>
<b><%= label_tag 'Select a shipping address:' %></b>
<%= select_tag 'address_id', options_for_select(user_address.collect{ |address| [address.full_street_address, address.id]},) %>
<b><%= label_tag 'Select a delivery plan:' %></b>
<%= select_tag 'plan',["One Time Order","Weekly Subscription"] %>
<%= hidden_field_tag 'chickenVegetables_quantity', chickenVegetables_quantity %>
<%= hidden_field_tag 'chickenRice_quantity', chickenRice_quantity %>
<%= hidden_field_tag 'beefCouscous_quantity', beefCouscous_quantity %>
<%= hidden_field_tag 'beefVegetables_quantity', beefVegetables_quantity %>
<%= hidden_field_tag 'user_id', current_user.id %>
<button id="btn-buy1" type="button" class="button i-submit">
Checkout
</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
email: '<%=current_user.email%>',
token: function(token, arg) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('btn-buy1').addEventListener('click', function(e) {
handler.open({
name: 'Name',
description: 'Purchase',
amount: document.getElementById("amount").value
});
e.preventDefault();
})
</script>
<% end %>
充电控制器:
class ChargesController < ApplicationController
def new
end
def create
if params[:plan] == 'One Time Order'
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
else
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken],
:plan => "meal_subscription",
:quantity => params[:chickenVegetables_quantity]
)
end
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => params[:amount],
:description => 'PetPlate customer',
:currency => 'usd'
)
purchase = Purchase.create(email: params[:stripeEmail], card: params[:stripeToken],
amount: params[:amount], description: charge.description, currency: charge.currency,
customer_id: customer.id, chickenVegetables_quantity: params[:chickenVegetables_quantity], chickenRice_quantity: params[:chickenRice_quantity],
beefCouscous_quantity: params[:beefCouscous_quantity],beefVegetables_quantity: params[:beefVegetables_quantity],user_id: params[:user_id], tax: params[:tax],
address: params[:address], apt_no: params[:apt_no], city: params[:city], state: params[:state], zip_code: params[:zip_code], phone_number: params[:phone_number],
restaurant_id: params[:restaurant_id], address_id: params[:address_id], plan: params[:plan])
redirect_to purchase
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
端
答案 0 :(得分:0)
如果您查看options_for_select
的文档,您会发现如果您需要在默认情况下选择一个选项,则必须将其作为第二个参数传递给options_for_select
所以在你的情况下,它会是这样的
<%= select_tag 'address_id', options_for_select([user_address.collect{ |address| [address.full_street_address, address.id]}],@selected_address.id) %> # if user address to be selected is @selected_address
<%= select_tag 'plan',options_for_select(["One Time Order","Weekly Subscription"],"Weekly Subscription")%> # to select 2nd option