Skip to content

Instantly share code, notes, and snippets.

@SunDi3yansyah
Last active November 9, 2018 16:49
Show Gist options
  • Save SunDi3yansyah/0d93584df34eec9cfd989b160f9d26db to your computer and use it in GitHub Desktop.
Save SunDi3yansyah/0d93584df34eec9cfd989b160f9d26db to your computer and use it in GitHub Desktop.
class Api::RegionsController < ApplicationController
def provinces
prompt = [
{
id: "",
name: core_model_lang("province")
}
]
@provinces = prompt + Province.all
render json: @provinces.to_json(only: [:id, :name])
end
def regency_id
@regency_id = Regency.where(province_id: params[:id])
render :regency_id, layout: false
end
def district_id
@district_id = District.where(regency_id: params[:id])
render :district_id, layout: false
end
def village_id
@village_id = Village.where(district_id: params[:id])
render :village_id, layout: false
end
end
if $('#province_id') and $('#province_id').is(':empty')
$.ajax
type: 'GET'
url: '/api/regions/provinces'
success: (data) ->
$.each data, (string, value) ->
$('#province_id').append '<option value="' + value.id + '">' + value.name + '</option>'
$('#province_id').change ->
province_id = $('#province_id').val()
$.ajax
type: 'POST'
url: '/api/regions/regency/' + province_id
success: (data) ->
$('#regency_id').html data
$('#regency_id').val ''
$('#regency_id').change ->
regency_id = $('#regency_id').val()
$.ajax
type: 'POST'
url: '/api/regions/district/' + regency_id
success: (data) ->
$('#district_id').html data
$('#district_id').val ''
$('#district_id').change ->
district_id = $('#district_id').val()
$.ajax
type: 'POST'
url: '/api/regions/village/' + district_id
success: (data) ->
$('#village_id').html data
$('#village_id').val ''
namespace :api do
get "regions/provinces", to: "regions#provinces", format: :json
post "regions/regency/:id", to: "regions#regency_id", format: "html"
post "regions/district/:id", to: "regions#district_id", format: "html"
post "regions/village/:id", to: "regions#village_id", format: "html"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment