访问关联集合中的下一个记录

时间:2021-04-13 15:53:38

I am new to Rails, trying to write a simple flashcard program where a user has a deck of vocabulary cards they are cycling through...

我是Rails的新手,我试着编写一个简单的抽认卡程序,在这个程序中,用户有一副他们正在循环使用的词汇卡……

The model is a very straightforward relationship between user and card where:

该模型是用户与card之间非常直接的关系,其中:

 User  has_many :cards
 Card  belongs_to :user

The basic gist is that a user views a card on the index page and clicks a button, "flipping" over to the show page, where the other side is displayed.

基本要点是,用户在索引页上查看卡片并单击一个按钮,“翻转”到显示页,显示另一侧。

Have created and seeded my AR database, currently rendering a functional version that accesses and "flips" card #1 in my deck, but am stuck up on accessing the second, third, fourth cards, etc.

已经创建和播种了我的AR数据库,目前正在呈现一个功能版本,可以访问和“翻转”我的牌堆中的第1张牌,但是在访问第2张、第3张、第4张牌时被卡住了,等等。

I have tried many different variations on AR queries in my Card controller to get the next cards in the deck but none have worked... here's what I've got in my card controller now:

我已经在我的卡控制器中尝试了许多不同的AR查询来获得牌组中的下一张牌,但是没有一张是有效的……这是我的卡控制器里的东西:

 def index
    @card = Card.all.next
    @cards = Card.all
  end
`

Here's my card model:

这是我的名片模型:

class Card < ActiveRecord::Base
  belongs_to :user

  def self.next
    n = 1 
    if self.id != Card.all.first.id
      Card.all.find_by(id: Card.all.first.id + n)
    end
    n += 1
  end

  validates :word_text, presence: true
  validates :meaning_text, presence: true
end

Here's my rake routes:

这是我的耙路线:

  Prefix Verb   URI Pattern                    Controller#Action
     root GET    /                              cards#index
    cards GET    /cards(.:format)               cards#index
         POST    /cards(.:format)               cards#create
 new_card GET    /cards/new(.:format)           cards#new
edit_card GET    /cards/:id/edit(.:format)      cards#edit
     card GET    /cards/:id(.:format)           cards#show
        PATCH    /cards/:id(.:format)           cards#update
          PUT    /cards/:id(.:format)           cards#update
       DELETE    /cards/:id(.:format)           cards#destroy
          GET    /cards/:id(.:format)           cards#show
`

..... So, because of the above, the code below is of course not doing what i want it to do, but here's my view page as of now:

.....因此,由于上面的原因,下面的代码当然没有做我想做的事情,但是这是我现在的视图页面:

<div id="front_page_container" class="medium-8 medium-centered text-center columns">

  <div class="row">
  </div>
</div>

<div id="box-container">

    <br> <br> <%= button_tag(type: 'button') do %>

    <h1 style="color:yellow"> <%= @card.word_text %>    

    <ul><%= link_to 'Tap to flip card', card_path(@card) %></ul>  
    <ul> <%= content_tag(:strong,'Tap to flip the card') %> </ul>

    <% end %></h1>

</div>

<br> <br> <%= button_tag(type: 'button') do %>

    <ul><%= link_to 'New Card', cards_path(@next) %></ul>  

    <ul> <%= content_tag(:strong,'New Card') %> </ul>

<% end %>

To be honest, I am very stuck on how to create a path from my index page (which is displaying card #1, or @card) BACK to a new index page where card #2, or @next, is displayed instead... any help would be much appreciated!

老实说,我非常纠结于如何创建一个路径,从我的索引页(显示的是card #1或@card)返回到一个新的索引页,其中显示的是card #2或@next……如有任何帮助,我们将不胜感激!

1 个解决方案

#1


2  

Get the @next card by doing the following

通过以下操作获取@next卡

@card = Card.find(params[:id])
@next = Card.where('id > ?', @card.id).first
@next = Card.first if @next.nil?

Remeber that when @card is the last card in your DB, you will want to handle that as well since @next would be nil in that case, which is the reason for the third line.

请记住,当@card是DB中的最后一张卡时,您也需要处理它,因为在这种情况下,@next将是nil,这就是第三行出现的原因。

Edit: To fix your specific code, you need to modify your next method in your model like this

编辑:要修改特定的代码,需要像这样修改模型中的下一个方法

def next  # This is a method on an INSTANCE of Card, not the Class
  next_card = Card.where('id > ?', self.id).first
  next_card = Card.first if next_card.blank?
  next_card
end

This method is then called on @card, not Card, so something like this

然后这个方法被调用到@card上,而不是Card,类似这样

<%= link_to 'New Card', card_path(@card.next) %>

#1


2  

Get the @next card by doing the following

通过以下操作获取@next卡

@card = Card.find(params[:id])
@next = Card.where('id > ?', @card.id).first
@next = Card.first if @next.nil?

Remeber that when @card is the last card in your DB, you will want to handle that as well since @next would be nil in that case, which is the reason for the third line.

请记住,当@card是DB中的最后一张卡时,您也需要处理它,因为在这种情况下,@next将是nil,这就是第三行出现的原因。

Edit: To fix your specific code, you need to modify your next method in your model like this

编辑:要修改特定的代码,需要像这样修改模型中的下一个方法

def next  # This is a method on an INSTANCE of Card, not the Class
  next_card = Card.where('id > ?', self.id).first
  next_card = Card.first if next_card.blank?
  next_card
end

This method is then called on @card, not Card, so something like this

然后这个方法被调用到@card上,而不是Card,类似这样

<%= link_to 'New Card', card_path(@card.next) %>