它在数据库中显示为零,这可以解决主要问题吗?
It shows nil in the database, can this solve the main issue?
如果卖家卖东西,我无法向购买它的用户显示。使用销售、产品和用户模型
<% @sales.each do |sale| %>
<tr>
<td><%= link_to sale.product.title, pickup_path(sale.guid) %></td>
<td><%= time_ago_in_words(sale.created_at) %> ago</td>
<td><%= sale.seller_email %></td>
<td><%= sale.amount %></td>
如果我继续将其更改为 <%= sale.buyer_email %>
,它只会向我显示当前用户以及他们刚买了什么,而不是谁买了他们的物品。这是我在检查控制台后得到的,seller_email
为零,最后一次销售的金额为零。我该如何解决这个问题,以便卖家可以看到谁去了他们的商品?
实际上,应该更改模型结构以创建正确的架构。
您有用户、产品和销售模型。所以协会应该像下面这样。
class User
has_many :products
has_many :sales
has_many :customers, :through => :sales
end
class Product
has_many :sales
belongs_to :user
has_many :buyers, :through => :sales
has_many :sellers, :through => :sales
end
class Sale
belongs_to :seller, :class_name => "User"
belongs_to :buyer, :class_name => "User"
belongs_to :product
end
然后您可以通过以下代码行访问该产品的所有买家和卖家。
product.buyers
product.sellers
您在 Transaction
控制器中的 current_user
似乎为零。
我看到您在 Transaction Controller
中遗漏了 before_action :authenticate_user!
。
因此您可以使用 pry 之类的调试 gem 来检查这一点,尝试在 product.sales.create!(buyer_email: current_user.email)
之前添加 binding.pry
并查看 current_user
有一个值
HTH
如果卖家卖东西,我无法向购买它的用户显示。使用销售、产品和用户模型
<% @sales.each do |sale| %>
<tr>
<td><%= link_to sale.product.title, pickup_path(sale.guid) %></td>
<td><%= time_ago_in_words(sale.created_at) %> ago</td>
<td><%= sale.seller_email %></td>
<td><%= sale.amount %></td>
如果我继续将其更改为 <%= sale.buyer_email %>
,它只会向我显示当前用户以及他们刚买了什么,而不是谁买了他们的物品。这是我在检查控制台后得到的,seller_email
为零,最后一次销售的金额为零。我该如何解决这个问题,以便卖家可以看到谁去了他们的商品?
实际上,应该更改模型结构以创建正确的架构。
您有用户、产品和销售模型。所以协会应该像下面这样。
class User
has_many :products
has_many :sales
has_many :customers, :through => :sales
end
class Product
has_many :sales
belongs_to :user
has_many :buyers, :through => :sales
has_many :sellers, :through => :sales
end
class Sale
belongs_to :seller, :class_name => "User"
belongs_to :buyer, :class_name => "User"
belongs_to :product
end
然后您可以通过以下代码行访问该产品的所有买家和卖家。
product.buyers
product.sellers
您在 Transaction
控制器中的 current_user
似乎为零。
我看到您在 Transaction Controller
中遗漏了 before_action :authenticate_user!
。
因此您可以使用 pry 之类的调试 gem 来检查这一点,尝试在 product.sales.create!(buyer_email: current_user.email)
之前添加 binding.pry
并查看 current_user
有一个值
HTH