What do the pickers mean? What values are they representing? When you choose one picker value, why does another picker need a different set of values to appear? If you can explain how you have modeled that part of your data, then I can help you further. In principle, a controller (any controller) can handle requests related to any model. It's just a convention that if you have a model Foo, you will have a FoosController to handle requests related to that model.
Here's a practical example of how you might use this sort of cascading picker:
class Country < ApplicationRecord
has_many :states
end
class State < ApplicationRecord
belongs_to :country
has_many :cities
end
class City < ApplicationRecord
belongs_to :state
end
You have a natural cascade of country to state to city, and it makes sense what those relationships are. The relationships are codified with the has_many and belongs_to macros, so it's clear that if you say @state.cities, you will get the cities that exist inside that state. You would do that by establishing what the parent id is in each child record. You might have Arizona as @state id = 1, and then have a bunch of cities like Phoenix: @city id = 22, state_id = 1, so you can gather them up.
So you have choices now as to choosing a controller pattern. The REST design says that you should have a separate controller for each model, and so that could go like this:
class CountriesController < ApplicationController
def index
@countries = Country.order(:name)
render partial: 'countries_picker', layout: false
end
def show
country = Country.find params[:id]
@states = @country.states.order(:name)
render partial: 'states_picker', layout: false
end
end
class StatesController < ApplicationController
def show
state = State.find params[:id]
@cities = @state.cities.order(:name)
render partial: 'cities_picker', layout: false
end
end
Your routes would look like this
resources :countries, only: [:index, :show]
resources :states, only: :show
If you wanted to have a single controller concerned with making these pickers (and ignore the whole REST thing), then you could have:
class PickersController < ApplicationController
def countries
@countries = Country.order(:name)
render partial: 'countries_picker', layout: false
end
def states
country = Country.find params[:id]
@states = country.states.order(:name)
render partial: 'states_picker', layout: false
end
def cities
state = State.find params[:id]
@cities = state.cities.order(:name)
render partial: 'cities_picker', layout: false
end
end
Your routes could look like this:
match '/countries', to: 'pickers#countries', via: :get, as: :countries_picker
match '/states', to: 'pickers#states', via: :get, as: :states_picker
match '/cities', to: 'pickers#cities', via: :get, as: :cities_picker
See -- there's no rules that say that you have to use one controller for one model, or have a separate controller for each model. Mix and match. Have fun! Rails is flexible.
Walter
> To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/CA%2B%3DMcKa2uFZFQHwLG_QKFfOxc%2B-BNXec%2Byw23d7U232172L%2BRg%40mail.gmail.com.