Reputation: 2520
Suppose, I have a model called Animal. This model contains enum attribute kind with two possible states.
class Animal < ActiveRecord::Base
enum kind: [ :cat, :dog ]
end
Then in my controller I create corresponding instance variables collections.
class AnimalsController < ApplicationController
def index
@cats = Animal.cat
@dogs = Animal.dog
end
end
In my view I got two links and two collections.
<h1>Animals</h1>
<b><%= link_to 'List Cats' %></b>
<b><%= link_to 'List Dogs' %></b>
<%= render partial: 'animals/cat', collection: @cats, as: :cat %>
<%= render partial: 'animals/dog', collection: @dogs, as: :dog %>
What is the prefered way of displaying first collection instead of second or second one instead of the first in the same place depending on clicked link? How to do that?
Upvotes: 0
Views: 43
Reputation: 1984
You can write following code to switch between different lists
<%= link_to "List Cats", animals_path(:cat => true) if params[:dog] %>
<%= link_to "List Dogs", animals_path(:dog => true) if params[:cat] %>
<div id="list">
<% if params[:cat] == true %>
<%= render partial: 'animals/cat', collection: @cats, as: :cat %>
<% elsif params[:dog] == true %>
<%= render partial: 'animals/dog', collection: @dogs, as: :dog %>
<% end %>
</div>
Upvotes: 1