learning_to_swim
learning_to_swim

Reputation: 361

Error - No route matches

I am building a sample app that lets users create a collection of "Word Groups" that contain multiple "Word Lists" which in turn contains multiple "Custom Words"

  #Word groups, word lists, and custom words
  resources :word_groups, shallow: true do
    resources :word_lists, shallow: true do
      resources :custom_words
    end
  end

My relevant routes -

word_group_word_lists GET    /word_groups/:word_group_id/word_lists(.:format)     word_lists#index
                      POST   /word_groups/:word_group_id/word_lists(.:format)      word_lists#create

/word_groups/index.html.erb

<% if current_user.word_groups.any? %>
    <ol class="word_groups">
        <%= render @word_groups %>
    </ol>   
<% end %>

/word_groups/_word_group.html.erb

<li>
    <span class="name panel-body"><%= word_group.name %></span>
    <span class="name panel-body"><%= word_group.id %></span>
    <span class="name panel-body"><%= link_to "Manage Lists", word_group_word_lists_path(word_group)  %></span>
</li>

Both word_group.name and word_group.id return the right values.

The word_group_word_lists_path errors out with - "Error - No route matches {:controller=>"word_lists", :word_group_id=>#}"

I sincerely appreciate any help you can provide!

UPDATE -

I added a condition to check if the word_group is not nil and not_new in my view and that resolved it.

<% if !word_group.nil? && !word_group.new_record? %>
    <li>
        <span class="name panel-body"><%= word_group.name %></span>
        <span class="name panel-body"><%= link_to "Manage Lists", word_group_word_lists_path(word_group)  %></span>
    </li>
<% end %>

Upvotes: 1

Views: 47

Answers (1)

benjaminjosephw
benjaminjosephw

Reputation: 4417

These routes look right and this seems like it should work. Perhaps try to explicitly call the id in the path like this:

word_group_word_lists_path(word_group.id)

Upvotes: 1

Related Questions