gpt4 book ai didi

ruby-on-rails - 隐藏基于 bool 值的键值对

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:18 26 4
gpt4 key购买 nike

我正在使用 Stripe 向我的用户收取辅导服务费用。目前,教练可以决定他想在他的设置中提供什么计划。

如果教练仅将 basic_coaching 设置为 false,那么这是唯一不应出现的 Stripe 按钮。

我该怎么做?

这是我的架构:

t.boolean "basic_coaching", default: true
t.boolean "professional_coaching", default: true
t.boolean "premium_coaching", default: true
t.boolean "platinum_coaching", default: true

这是我的 Controller :

@usd_plans = { # Plan ID -> Description
'basic_usd': 'Basic Coaching',
'professional_usd': 'Professional Coaching',
'premium_usd': 'Premium Coaching',
'platinum_usd': 'Platinum Coaching'
}

这是我的看法:

- @usd_plans.each do |plan_id, description|
td
= form_tag(subscriptions_path) do
= hidden_field_tag 'plan', plan_id
= hidden_field_tag 'coach_id', @user.id
= hidden_field_tag 'user_id', current_user.id
= hidden_field_tag 'description', description
- if current_user.coupon_code
= hidden_field_tag 'coupon_code', current_user.coupon_code

script.stripe-button(
src='https://checkout.stripe.com/checkout.js'
data-key=Rails.configuration.stripe[:publishable_key]
data-name="#{@user.name}"
data-description=description
data-image="#{@user.avatar.url}"
data-locale='auto'
data-panel-label='Subscribe now'
data-label='Buy'
data-allow-remember-me='false'
data-email=current_user.email
)

这是所有 4 个 bool 值都设置为 true 时的样子。理想情况下,如果该特定 bool 值设置为 false,我会隐藏一个按钮。

enter image description here

最佳答案

您可以决定何时构建@usd_plans,例如像这样的东西:

@usd_plans = {}.tap do |plans| # Plan ID -> Description
plans['basic_usd'] = 'Basic Coaching' if coach.basic_coaching?
plans['professional_usd'] = 'Professional Coaching' if coach.professional_coaching?
plans['premium_usd'] = 'Premium Coaching' if coach.premium_coaching?
plans['platinum_usd'] = 'Platinum Coaching' if coach.platinum_coaching?
end

当然,您可以通过一些字符串连接和元编程让它变得更干。重复的版本更利于理解:)。

编辑:如果您想保留布局,请尝试将其隐藏在 View 中

@usd_plans = {}.tap do |plans| # Plan ID -> Description
plans['basic_usd'] = {name: 'Basic Coaching', visible: coach.basic_coaching? }
# etc...
end

- @usd_plans.each do |plan_id, data|
td
next unless data[:visible]
= form_tag(subscriptions_path) do
= hidden_field_tag 'plan', data[:plan_id]
= hidden_field_tag 'coach_id', @user.id
= hidden_field_tag 'user_id', current_user.id
= hidden_field_tag 'description', description
- if current_user.coupon_code
= hidden_field_tag 'coupon_code', current_user.coupon_code

script.stripe-button(
src='https://checkout.stripe.com/checkout.js'
data-key=Rails.configuration.stripe[:publishable_key]
data-name="#{@user.name}"
data-description=description
data-image="#{@user.avatar.url}"
data-locale='auto'
data-panel-label='Subscribe now'
data-label='Buy'
data-allow-remember-me='false'
data-email=current_user.email
)

关于ruby-on-rails - 隐藏基于 bool 值的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49319366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com