0
目前,如果某人點擊了link_to
,他們將被帶到上一個或下一個挑戰。通過@用戶對象的下一個和上一個?
但是link_to
如何工作,只有@user
的挑戰包括在內?
視圖
<% if @challenge.previous %>
<%= link_to 'Prev', challenge_path(@challenge.previous), class: "footer-left", style: "color: white; font-style: normal;" %>
<% else %>
<%= link_to 'Home', root_url, class: "footer-left" %>
<% end %>
<% if @challenge.next %>
<%= link_to 'Next', challenge_path(@challenge.next), class: "footer-right" %>
<% else %>
<%= link_to 'Home', root_url, class: "footer-right" %>
<% end %>
模式
def next
Challenge.where("id > ?", id).first # I get nil error for... Challenge.find_by(user_id: @user.id).where("id > ?", id).first
end
def previous
Challenge.where("id < ?", id).last # I get nil error for... Challenge.find_by(user_id: @user.id).where("id > ?", id).last
end
我知道@user
沒有在模型中工作,但我只是用它作爲一個例子試圖讓User
他們所屬的挑戰。
用戶和挑戰之間是否存在'has_many'關係? – mysmallidea
它是否曾經工作過,或者只有當你遇到最後一個挑戰時 –
'@ user.challenges.where('id>?',@ challenge.id).first'應該用下一個更高的ID返回挑戰,假設一個用戶面臨很多挑戰。 – Stefan