renatojf
renatojf

Reputation: 744

Where should I create an action to populate a select?

I'm quite new to Rails development and I came up with this question today. I have a method that returns some JSON data.

It is used to populate a select with a list of cities according to what was selected on a previous select (list of states). So, it's a simple method that loads a list based on some ajax parameter passed through and it is used all along my site.

I'm using Rails 4 and I placed this method on my HomeController. So everytime I need to fetch the list of cities, I call the HomeController to load the data.

Is this the correct approach or should I place this method on a more generic controller (like ApplicationController)? Is there a better way?

Upvotes: 0

Views: 36

Answers (1)

Rebitzele
Rebitzele

Reputation: 3282

I think the best thing is to keep this modular. So you can create a separate controller for this, like StatesController - and possibly even a separate model if that makes sense for your application (I'm not sure where you're getting your data). There is no cost to having extra controllers, and this way your code is clean and organized, with each piece of functionality existing in its logical place.

Upvotes: 2

Related Questions