Accessing the next record in a related collection
I'm new to Rails, trying to write a simple flashcard program where the user has a deck of lexical cards that they do on a bike ...
The model is a very simple connection between the user and the map, where:
User has_many :cards
Card belongs_to :user
The main point is that the user views the map on the index page and clicks a button, "flips" to the display page, where the other side is displayed.
Created and seeded my AR database, currently a functional version that refers to "card" # 1 in my deck, but I'm stuck on accessing the second, third, fourth cards, etc.
I've tried many different variations of AR requests in the Card controller to get the next cards in the deck, but none worked ... here's what I currently have in my card controller:
def index
@card = Card.all.next
@cards = Card.all
end
`
Here's my map 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 certainly doesn't do what I want, but here's my view page at the moment:
<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'm very fixated on how to create a path from my index page (which displays card # 1 or @card) BACK to a new index page where card # 2 or @next is displayed ... any help would be much appreciated!
source to share
Get the @next card by doing the following
@card = Card.find(params[:id])
@next = Card.where('id > ?', @card.id).first
@next = Card.first if @next.nil?
Remember that when @card is the last card in your DB, you will need to handle this since @next will be null in this case, which is the reason for the third line.
Edit: To fix your specific code, you need to change the following method in your model as follows:
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 and not on the card, so something like this
<%= link_to 'New Card', card_path(@card.next) %>
source to share