在显示用户的购买时出现错误,当显示他购买的产品时,我得到产品图片和标题的错误 [英] Getting error when showing the purchases of the user,when showing the product he bought i get an error for product image and title

查看:143
本文介绍了在显示用户的购买时出现错误,当显示他购买的产品时,我得到产品图片和标题的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在User,Gig(产品)和记录购买的购买表之间的关系。

 用户< ActiveRecord :: Base 
has_many:gigs
has_many:purchases,foreign_key:'buyer_id'
has_many:sales,foreign_key:'seller_id',class_name:'Purchase'
end

class Gig< ActiveRecord :: Base
has_many:purchases
has_many:buyer,through::purchases
has_many:sellers,through::purchases
end

< ActiveRecord :: Base
belongs_to:gig
belongs_to:buyer,class_name:'User'
belongs_to:seller,class_name:'User'
end

记录我在控制器中使用的购买

  def downloadpage 
ActiveRecord :: Base.transaction do
if current_user.points> = @ gig.pointsneeded
@purchase = current_user.purchases.create(gig:@gig, seller:@ gig.user)
如果@purchase
current_user.points - = @ gig.pointsneeded
@ gig.user.points + = @ gig.pointsneeded
current_user.save
if @ gig.user.save
render'successful_download',local:{link:@ gig.boxlink}
end
end
else
redirect_to :back,notice:你没有足够的点数
end
end
end

$ b $




$ b

一切正常,当买家从卖家处购买东西时,分数会在帐户之间转移。 >

 < h1>您已下载<%= current_user.purchases.count%>框< / h1> 

它会显示买家发出的gigs数量。



现在我想显示的不只是数字,而是他买的产品的标题和图片。这是我试过

 < div class =row experiment> 
<%current_user.purchases.each do | gig | %>
< div class =well for-h1-gig-second col-xs-6 col-sm-4 col-lg-3>
<%= link_to(image_tag gig.image.url(:medium),:class =>img-responsive),gig%>
< h1><%= link_to gig.title,gig%>< / h1>
< / div>
<%end%>
< / div>

但它说,它找不到图像和标题。
所以我试过 current_user.purchases.gig.each do | gig |
没有成功。


我如何解决它?
PS:请随时修改我的标题,为将来的读者,我不能更好地制定它,谢谢你。



current_user.purchases.each 通过购买而不是演示来重复。

 < div class =row experiment> 
<%current_user.purchases.each do | purchase | %>
<%gig = purchase.gig%>
< div class =well for-h1-gig-second col-xs-6 col-sm-4 col-lg-3>
<%= link_to(image_tag gig.image.url(:medium),:class =>img-responsive),gig%>
< h1><%= link_to gig.title,gig%>< / h1>
< / div>
<%end%>
< / div>

还有一些其他问题:

  class User< ActiveRecord :: Base 
has_many:gigs#不工作。
end

它不工作的原因是 user gig 通过购买 buyer_id seller_id 外键。 Rails不支持依赖于多个键的关系。



如果要选择用户是卖家或买家的演出,您可以使用:

  Gig.joins(:purchases).where('purchases.buyer_id =?OR purchases.seller_id =?',[current_user.id,current_user.id ])


This is the relationship i have between User,Gig(product),and Purchase table that records the purchase.

class User < ActiveRecord::Base
  has_many :gigs
  has_many :purchases, foreign_key: 'buyer_id'
  has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'
end

class Gig < ActiveRecord::Base
  has_many :purchases
  has_many :buyers, through: :purchases
  has_many :sellers, through: :purchases
end

class Purchase < ActiveRecord::Base
  belongs_to :gig
  belongs_to :buyer, class_name: 'User'
  belongs_to :seller, class_name: 'User'
end

To record the purchase i use in my controller

 def downloadpage
    ActiveRecord::Base.transaction do
      if current_user.points >= @gig.pointsneeded 
        @purchase = current_user.purchases.create(gig: @gig, seller: @gig.user)
        if @purchase
          current_user.points -= @gig.pointsneeded
          @gig.user.points += @gig.pointsneeded
          current_user.save
          if @gig.user.save
            render 'successful_download', locals:{link:@gig.boxlink}
          end
        end
      else
        redirect_to :back, notice: "You don't have enough points"
      end
    end
  end

everything works when the buyer buys something from the seller,the points are transferred between accounts,and the buyer gets redirected to the final gig.

In my views i can do

<h1>You downloaded <%= current_user.purchases.count %> boxes</h1>

It will show the number of "gigs" the buyer made.

Now i want to show not just the number,but the title and the picture of the product he bought.This is what i tried

<div class="row experiment">
      <% current_user.purchases.each do |gig| %>
      <div class="well for-h1-gig-second col-xs-6 col-sm-4 col-lg-3 ">
        <%= link_to (image_tag gig.image.url(:medium), :class=>"img-responsive"), gig %>
        <h1><%= link_to gig.title, gig %></h1>
      </div>
      <% end %>
    </div>

But it says,that it can not find the image and the title. so i tried current_user.purchases.gig.each do |gig| without success.

How do i fix it? P.S: Please feel free to edit my title,for future readers,i couldn't formulate it better,thank you.

解决方案

Your main problem is that current_user.purchases.each iterates through purchases - not gigs.

<div class="row experiment">
    <% current_user.purchases.each do |purchase| %>
    <% gig = purchase.gig %>
    <div class="well for-h1-gig-second col-xs-6 col-sm-4 col-lg-3 ">
        <%= link_to(image_tag gig.image.url(:medium), :class=>"img-responsive"), gig %>
        <h1><%= link_to gig.title, gig %></h1>
    </div>
    <% end %>
</div>

Also to some of the other issues:

class User < ActiveRecord::Base
  has_many :gigs # not going to work.
end

The reason it's not going to work is that the relation between user and gig goes through purchases and the buyer_id and seller_id foreign keys. Rails does not support relations that depend on multiple keys.

If you want to select gigs where the user is either seller or buyer you could use:

Gig.joins(:purchases).where('purchases.buyer_id=? OR purchases.seller_id=?', [current_user.id, current_user.id])

这篇关于在显示用户的购买时出现错误,当显示他购买的产品时,我得到产品图片和标题的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆