mercredi 13 février 2019

Nested Resources, passing value to inner index only passes value of 1

Im trying to display the list of words per category. Lets say category(1):Fruits has many words: apple(1), banana(2), grapes(3). category(2):Animals has many words: dog(4), cat(5)

In order to know if i passed the correct object i display the id. but it only passes the first object (category 1)

Im using nested resource, i tried the different formats and tried different parameters to pass and only get errors

This is the routes:

    namespace :admin do
       resources :categories do 
       resources :words
      end
    end

<% @categories.each do |category| %>
    <tr class="d-flex">
      <td class="col-2"><%= category.title %></td>
      <td class="col-6"><p id="description"><%= category.description %></p></td>
      <th class="col-1"><center><%= category.words.count %></center></th>
      <td class="col-3">
         <center>
              <%= link_to category.id, admin_category_words_path(category), class: "btn btn-primary btn-sm" %>
         </center>
      </td>
     </tr>
 <% end %>

When i pass admin_category_words_path(category) = the index /admin/category/n/words

the function in wordscontroller

def index 
        @category = Category.find_by(params[:category_id])
        #@words = @category.words.all <--- will display all the words of this category 
end

it displays the corresponding id on the url but when i display the id its still 1

but when i want is to get the correct object.

Aucun commentaire:

Enregistrer un commentaire