Forked from SunDi3yansyah/api\regions_controller.rb
Created
November 9, 2018 16:49
-
-
Save achmiral/63e37acaca41807c0e818dffbd7478c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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