所以我试着让我的应用程序在用户购买课程时看到一个查看模块按钮,因为当他们购买课程时,订单会被创建,所以我正在检查订单是否存在。
然而,目前当一个课程被购买时,视图模块会显示在所有课程上,但我希望它只显示在购买的课程上。
以下是我到目前为止所拥有的:
schema.rb
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未购买

购进

电子邮件营销已经购买,但他们都显示了“查看模块”按钮。
下面是我做按钮的方法
<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>发布于 2018-08-20 14:10:42
这一行:
if Order.exists?将检查是否存在任何订单。也就是说,只要订单表中至少有一条记录,它就会是真的。但是,您希望检查特定的订单,即当前用户是否订购了特定的课程。因此,您可以将其替换为:
if Order.exists?(user: current_user, course: course)但是,这将运行一个单独的查询,以检查每个课程是否存在订单。一旦你有了几门以上的课程,这就会变得效率低下。因此,最好运行一个查询来查找当前用户所订购的所有课程it,然后根据该列表进行检查。
https://stackoverflow.com/questions/51932437
复制相似问题