Search code examples
ruby-on-railsrails-activerecordrenderpartial

Rendering Partial with specific :category_id


I created a Categories Scaffold. User's can Upload an image and add a category to it. The Categories i created in the Scaffold are shown in a dropdown for users to Select.

On the Image Show Page i link to the category ->

<%= link_to @pin.category.name, @pin.category%>

It takes me to (for example)

localhost:3000/categories/1

On the Category Show Page i want to display all the images which share the Same Category.

How do i do this ?

Solution

  • In the Models you define that Catagory

    has_many :images

    and Image

    belongs_to :category

    • image should have category_id member of course

    Then , when you have category object in hand (let's say @cat), you simply iterate @cat.images

    You can read more about Models and Relation in here

    http://guides.rubyonrails.org/association_basics.html