r-pay-ō.com

Rails – Organizing the Index page and searching by topic

All of the companies in my custom CRM have a topic associated with them – these range from “Architecture” to “Outdoor” companies. Several users want to be able to view a list of companies under each category before they begin compiling their own lead list of companies to contact so today we’ll set up an option on our index page to enable that action.

We’re going to start off by creating the appropriate view on our index page. To keep our app/views/companies/index.html.erb page from growing too cluttered we’re going to create a partial labeled _topics.html.erb. We want it to load at the top of the page so we’ll place it above the code that lists out each company.

app/views/companies/index.html.erb
<%= render 'topics' %>

We’ll then create our new partial _topics.html.erb. MY company uses 9 different topics, so using bootstrap we’ll create 3 rows of 3 topics. Since each company has a topic associated with it in the database, along with each topic we’ll be passing along the params[:topic] with the appropriate topic.

Now that we’ve got the front end set up we actually need to get the back end up and running so our viewers aren’t confronted with a series of errors. Since our topic buttons will be on the index page, that’s where we’ll need to add our code in the companies controller. When a user first logs in I have the root set to the companies index page and I want to just list all of the companies in the crm at that point. You can set up the companies index page as root by entering the following code in your routes file:

config/routes.rb
root 'companies#index'

Next we’ll set up the companies controller index method to at first display all of the companies in the crm, unless a topic has been selected and then to just display all of the companies listed within that topic: