← Back to Upcase

Intro to Rails: Final lesson - Flashcards application, linking to guess controller in view


(Christopher Patti) #1

I’m having trouble wiring up the view for my new guess controller into the cards view of the deck.

I have:

  <div>
    <%= link_to card.front, [@deck, card] %>
    <%= link_to "Guess", show_guess_path(@deck, card) %>
    <%= link_to "Edit", edit_deck_card_path(@deck, card) %>
    <%= link_to "Delete", [@deck, card], method: :delete, confirm: "Are you sure?" %>
  </div>

and am getting:

 NoMethodError in Decks#show

Showing /Users/cpatti/src/intro-rails-ruby/flashcards/app/views/decks/show.html.erb where line #7 raised:

undefined method `show_guess_path' for #<#<Class:0x007fc7b76da228>:0x007fc7b75bc1e8>

Extracted source (around line #7):

4: <% @cards.each do |card| %>
5:   <div>
6:     <%= link_to card.front, [@deck, card] %>
7:     <%= link_to "Guess", show_guess_path(@deck, card) %>
8:     <%= link_to "Edit", edit_deck_card_path(@deck, card) %>
9:     <%= link_to "Delete", [@deck, card], method: :delete, confirm: "Are you sure?" %>
10:   </div>

I also tried deck_guess_path.

My routes look like this:

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 
    resource :guess, only: :show
  end
end

I don’t understand why things like edit_deck_card_path works and deck_guess_path doesn’t.

I suspect I’m missing some fundamental connection between routing, controllers, and automatic path generation.

My entire repository is at https://github.com/feoh/intro-rails-ruby

Thanks for any clues you may have.
-Chris


(Luís Ferreira) #2

@Feoh The helper for the show action is deck_guess_path, and it only needs the id of the deck, since you’re using resource and not resources for the guess, which means there is only one for each deck.

So it should be: deck_guess_path(@deck)

Also, when running into these kind of problems you might want to run rake routes on the terminal.


(Christopher Patti) #3

@zamith thanks a bunch that worked perfectly. I totally forgot about rake routes, that proved very instructive. Turns out I wanted resources and not just resource because a guess has to be on a particular card.


(Luís Ferreira) #4

@Feoh Glad to hear that.