在 Rails 4 中显示用户 "buy now" 或 "edit gig" 或 "download button"
Show the user "buy now" or "edit gig" or "download button" in Rails 4
这是我目前拥有的代码
<% if user_signed_in? && @gig.user == current_user %>
<%= link_to "Edit your gig", edit_gig_path %>
<% else %>
<%= link_to "Get this gig for #{@gig.pointsneeded} points", download_path(@gig)%>
<% end %>
第一行说 if 用户已登录且演出归当前用户所有 Show "Edit your gig" ,
或者 else 如果演出不属于用户,请向他显示 "Get this gig for (a number) of points".
这是我的模特关系
对于Purchase.rb
class Purchase < ActiveRecord::Base
belongs_to :gig
belongs_to :buyer, class_name: 'User'
belongs_to :seller, class_name: 'User'
end
对于User.rb
class User < ActiveRecord::Base
has_many :purchases, foreign_key: 'buyer_id'
has_many :gigs, through: :purchases, source: :buyer
has_many :gigs
has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'
end
对于Gig.rb
class Gig < ActiveRecord::Base
has_many :purchases
has_many :buyers, through: :purchases
has_many :sellers, through: :purchases
belongs_to :user
end
Question: How to make the logic so that,if the user owns the gig show "Edit your gig",elsif if the user purchased the the gig
from another user and it's in his current_user.purchases
list,show
"you already purchased this gig",else "buy for 20 points".
完整解:
<% if user_signed_in? && @gig.user == current_user %>
<%= link_to "Edit your box", edit_gig_path%>
<% elsif @gig.buyers.pluck(:id).include?(current_user.id) %>
<%= link_to "you already purchased this gig", edit_gig_path%>
<% else %>
<%= link_to "Get this box for #{@gig.pointsneeded} points", download_picture_path(@gig)%>
<% end %>
首先,更改此设置以便@gig 不会调用 sql 来加载用户,而是比较演出的 user_id:
<% if user_signed_in? && @gig.user_id == current_user %>
比较 id 而不是实际对象将使此页面更快。
要查看 current_user 是否购买了演出,请执行以下操作:
<% if @gig.buyers.pluck(:id).include?(current_user.id) %>
这是我目前拥有的代码
<% if user_signed_in? && @gig.user == current_user %>
<%= link_to "Edit your gig", edit_gig_path %>
<% else %>
<%= link_to "Get this gig for #{@gig.pointsneeded} points", download_path(@gig)%>
<% end %>
第一行说 if 用户已登录且演出归当前用户所有 Show "Edit your gig" , 或者 else 如果演出不属于用户,请向他显示 "Get this gig for (a number) of points".
这是我的模特关系
对于Purchase.rb
class Purchase < ActiveRecord::Base
belongs_to :gig
belongs_to :buyer, class_name: 'User'
belongs_to :seller, class_name: 'User'
end
对于User.rb
class User < ActiveRecord::Base
has_many :purchases, foreign_key: 'buyer_id'
has_many :gigs, through: :purchases, source: :buyer
has_many :gigs
has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'
end
对于Gig.rb
class Gig < ActiveRecord::Base
has_many :purchases
has_many :buyers, through: :purchases
has_many :sellers, through: :purchases
belongs_to :user
end
Question: How to make the logic so that,if the user owns the gig show "Edit your gig",elsif if the user purchased the the gig from another user and it's in his
current_user.purchases
list,show "you already purchased this gig",else "buy for 20 points".
完整解:
<% if user_signed_in? && @gig.user == current_user %>
<%= link_to "Edit your box", edit_gig_path%>
<% elsif @gig.buyers.pluck(:id).include?(current_user.id) %>
<%= link_to "you already purchased this gig", edit_gig_path%>
<% else %>
<%= link_to "Get this box for #{@gig.pointsneeded} points", download_picture_path(@gig)%>
<% end %>
首先,更改此设置以便@gig 不会调用 sql 来加载用户,而是比较演出的 user_id:
<% if user_signed_in? && @gig.user_id == current_user %>
比较 id 而不是实际对象将使此页面更快。
要查看 current_user 是否购买了演出,请执行以下操作:
<% if @gig.buyers.pluck(:id).include?(current_user.id) %>