首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果购买Stripe,只显示按钮

如果购买Stripe,只显示按钮
EN

Stack Overflow用户
提问于 2018-08-20 14:01:45
回答 1查看 31关注 0票数 0

所以我试着让我的应用程序在用户购买课程时看到一个查看模块按钮,因为当他们购买课程时,订单会被创建,所以我正在检查订单是否存在。

然而,目前当一个课程被购买时,视图模块会显示在所有课程上,但我希望它只显示在购买的课程上。

以下是我到目前为止所拥有的:

schema.rb

代码语言:javascript
复制
  create_table "courses", force: :cascade do |t|
    t.string "title"
    t.text "summary"
    t.text "description"
    t.string "trailer"
    t.integer "price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "slug"
    t.index ["slug"], name: "index_courses_on_slug", unique: true
  end

  create_table "orders", force: :cascade do |t|
    t.integer "user_id"
    t.integer "course_id"
    t.float "total"
    t.index ["course_id"], name: "index_orders_on_course_id"
    t.index ["user_id"], name: "index_orders_on_user_id"
  end

未购买

购进

电子邮件营销已经购买,但他们都显示了“查看模块”按钮。

下面是我做按钮的方法

代码语言:javascript
复制
<section class="flex justify-between px-6 py-4">
    <% if user_signed_in? %>
      <% if current_user.isAdmin? %>
        <%= link_to "Edit", edit_course_path(course), class: "inline-block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
      <% else %>
        <% if Order.exists? %>
          <%= link_to "View Modules", course, class: "block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
        <% else %>
          <%= form_with(url: '/payments/create') do |f| %>
            <script
              src="https://checkout.stripe.com/checkout.js" class="stripe-button"
              data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
              data-image="https://s3.eu-west-2.amazonaws.com/aurameir-courses/aurameir-logo.png"
              data-name="<%= course.title %>"
              data-description="<%= course.summary %>"
              data-amount="<%= course.price*100 %>"
              data-label="Buy Now"
              >
            </script>
            <%= hidden_field_tag(:course_id, course.id) %>

            <%= f.submit "Buy now", class: "bg-blue hover:bg-blue-dark w-full text-white font-semibold py-3 px-4 border-2 rounded-sm border-blue-dark shadow outline-none" %>
          <% end %>
        <% end %>
      <% end %>
    <% else %>
      <%= link_to "View", course, class: "block text-lg text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
      <span class="inline-block bg-green font-bold text-xl text-white border-2 border-green-dark px-4 py-2 leading-none shadow">£<%= course.price %></span>
    <% end %>
  </section>
</section>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 14:10:42

这一行:

代码语言:javascript
复制
if Order.exists?

将检查是否存在任何订单。也就是说,只要订单表中至少有一条记录,它就会是真的。但是,您希望检查特定的订单,即当前用户是否订购了特定的课程。因此,您可以将其替换为:

代码语言:javascript
复制
if Order.exists?(user: current_user, course: course)

但是,这将运行一个单独的查询,以检查每个课程是否存在订单。一旦你有了几门以上的课程,这就会变得效率低下。因此,最好运行一个查询来查找当前用户所订购的所有课程it,然后根据该列表进行检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51932437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档