Edit: Sorry for the crappy formatting, I tried using ‘’‘ruby’‘’ and ‘’‘html’‘’ but it didn’t do anything?
So I added a guessing component to the app we build in the intro to rails workshop and I’m wondering if I went about it in the right way.
First I added a one line form next to each card so you can take a guess:
view/decks.show.html.erb
<% @cards.each do |card| %>
<div>
<strong><%= link_to card.front, [@deck, card] %></strong>
-
<%= link_to "Edit", edit_deck_card_path(@deck, card) %>
<%= link_to "Delete", [@deck, card], method: :delete, confirm: "You sure?" %>
<%= form_for(:guess, url: guess_path) do |form| %>
<%= form.text_field :guess, placeholder: "Guess the #{@deck.name} word for #{card.front}", size: 50 %>
<%= form.hidden_field :deck, value: @deck.id %>
<%= form.hidden_field :card, value: card.id %>
<%= form.submit "Guess" %>
<% end %>
<br>
</div>
<% end %>
I had to create the hidden fields so that I could redirect to the proper
card#show page in the guess controller I created:
class GuessesController < ApplicationController
def create
@deck = Deck.find(params[:guess][:deck])
@card = Card.find(params[:guess][:card])
cookies[:guess] = params[:guess][:guess]
redirect_to deck_card_path(@deck, @card)
end
end
I stored the guess in a cookie since that was the only way I could think of to pass around and object that’s not in the database (just like we did with sessions).
Then I created a guess_correct? helper method that could be used in the card view:
application_controller.rb
def guess_correct? guess, answer
guess.downcase == answer.downcase
end
helper_method :guess_correct?
And here’s the view where it is used:
views/cards/show.html.erb
<% if guess_correct? cookies[:guess], @card.back %>
<h1><%= @card.back %> is correct!</h1>
<% else %>
<h1><%= cookies[:guess] %> is incorrect.</h1>
<h2><%= @card.back %> is <%= @deck.name %> for <%= @card.front %></h2>
<% end %>
<br>
<div><%= link_to "#{@deck.name} cards", @deck %></div>
Well it works but it seems weird to me like I ‘hacked’ it together. I guess I’m wondering:
- Is storing the value of the guess in a cookie the right way to do go about it? It doesn’t need to persist in the database but it does need to persist for the guess_correct? method for use in the view.
- Did I really need to create the hidden form fields in order to have the proper redirect after going to the guess#create action?
- Has anyone else done this? I’d love to see other implementations.
Thanks thoughtbotters!