← Back to Upcase

Thoughts after finishing Intro to Ruby on Rails course


(Austin David) #1

I completed the course two days ago and pushed the flashcard app to my Github and I wanted to share my experience here:

First, off my hats off to @halogenandtoast for the great workshop and tips. It has solidified my weak foundation of writing Ruby and Rails apps. I found following his instructions and the example apps very easy and was never lost.

Even though only thoughtbotters can look at my apps on my private forked repos, I did want to provide some code that deals with @halogenandtoast’s challenge of adding the guessing card feature to the flashcard app. I initially had some trouble with it and wanted see if there was a simpler way to do the same thing.

First, routes.rb:

config/routes.rb

Flashcards::Application.routes.draw do
  root to: "homes#index"

  resource :session, only: [:new, :create, :destroy]
  resources :users, only: [:new, :create]

  resources :decks do
    resources :cards, except: :index do
      resource :guess, only: [:new, :create]
      get "/guess" => "guesses#show", as: :show_guess
    end
  end
end

I didn’t like the idea of nesting routes beyond two levels deep because it makes the routes paths look unwieldly but I didn’t want to persist the guess of a card wanted to make sure guess would be associated with cards and decks. Also notice, I had to manually add a named route for the show action because I wanted the user to go to a separate page to see the answer and I couldn’t find a way to do it within the create action. (or should I even try to do it with create?)

Next, my GuessesController

app/controllers/guesses_controller.rb

class GuessesController < ApplicationController
  before_filter :authenticate

  def show
    @deck = find_deck
    @card = find_card(@deck)
  end

  def new
    @deck = find_deck
    @card = find_card(@deck)
  end

  def create
    @deck = find_deck
    @card = find_card(@deck)
    @guess = params[:guess][:guess]
    if correct?(@guess, @card)
      redirect_to deck_card_show_guess_path
    else
      redirect_to new_deck_card_guess_path
    end
  end

  private

  def find_deck
    current_user.decks.find(params[:deck_id])
  end

  def find_card(deck)
    deck.cards.find(params[:card_id])
  end

  def correct?(guess,card)
    guess == card.back.downcase
  end
end

The create action was what gave me trouble here. As I mentioned above, I didn’t want to create a Guess model, so I just let the controller take care of holding on to the guesses like the sessions in SessionsController. Oddly enough, even though it only has three actions, I had the toughest time with figuring this out. It works but I feel it could be better. Also, do I really need a show action to show the answer or could I’ve done it with just the new and create actions. Notice I just redirect back to the new action if the guess is wrong. There must be a better way to do this. I just wasn’t sure how to do it.

Finally, new.html.erb

app/views/guesses/new.html.erb

Guesses Page
<p>Guess the <%= @deck.name %> word for <%= @card.front %>: </p>

<%= form_for(:guess, url: deck_card_guess_path) do |form| %>

  <li><%= form.label :guess %>
  <%= form.text_field :guess %></li>

<%= form.submit %>
<%= link_to "Back to Deck", deck_path(@deck) %>
<% end %>

Even though its a simple form, the difficulty I had with this go hand in hand the my routes.rb file above.

Sorry for the long post but your feedback is important to me as I’m trying to change careers by taking these workshops on Prime. Any advice is greatly appreciated


(Garland William Binns III) #2

@austindavid thanks for sharing and a huge pat on the back to you on your decision to change careers and take the initiative on stuff like this. go for it, full steam ahead! :slight_smile:

i noticed this hadn’t been replied to yet - definitely a lot of material here - maybe this would be something you could ask about during the office hours on friday?