我正在使用Stripe向我的用户收取辅导服务费用。目前,教练可以决定他想要在他的设置中提供什么计划。
如果教练只将basic_coaching设置为false
,那么这是唯一应该不的条纹按钮。
我该怎么做?
这是我的架构:
t.boolean "basic_coaching", default: true
t.boolean "professional_coaching", default: true
t.boolean "premium_coaching", default: true
t.boolean "platinum_coaching", default: true
这是我的控制器:
@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个布尔值设置为true
的情况。理想情况下,我只是隐藏一个按钮,如果该特定布尔值设置为false
。
答案 0 :(得分:3)
您可以决定构建@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
你当然可以通过一些字符串连接和元编程来使它更干。重复版本更适合理解:)。
编辑:如果您想保留布局,请尝试在视图中隐藏它
@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
)
答案 1 :(得分:1)
根据您对@ mrzasa的好答案的评论,我建议您可能需要在视图中处理逻辑,因此如果不需要计划按钮,可以添加占位符。< / p>
类似的东西:
按原样保留@usd_plans
:
@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
- if @user.try(description.underscore) # or description.downcase.split(' ').join('_'), I haven't got Rails handy to test `underscore`
= 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
)
这种方式,至关重要的是,td
标记将被插入,而按钮仅在用户具有该属性集时插入。
尝试一下,让我知道你是怎么过的 - 很乐意帮助解决任何问题。