Commit 18a2d206 authored by marcheing's avatar marcheing

Add state select action for profile form

Signed-off-by: Diego de Araújo Martinez Camarinha's avatarDiego Araújo <[email protected]>
parent 7155734e
var state_select = function(event) {
$.get('/profiles/states',
{ country: $('select#profile_country :selected').text() },
function(data) { $('div#state_select').html(data); }
);
}
$(document).ready(state_select);
$('select#country').on('change', state_select);
......@@ -62,6 +62,11 @@ class ProfilesController < ApplicationController
end
end
def states
states = Carmen::Country.named(states_params).subregions
render partial: 'profiles/states', locals: { states: states }
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
......@@ -76,4 +81,8 @@ class ProfilesController < ApplicationController
def set_carmen_locale
Carmen.i18n_backend.locale = I18n.locale.to_s.split('-').first
end
def states_params
params.require(:country)
end
end
<div class="field">
<%= label_tag I18n.t 'state' %>
<%= select_tag :state, state_select(states) %>
</div><br>
......@@ -4,6 +4,8 @@ Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :profiles
get 'states' => 'profiles#states', as: 'states'
root to: 'home#index'
end
end
......@@ -156,4 +156,22 @@ RSpec.describe ProfilesController, type: :controller do
end
end
describe 'states' do
render_views
let!(:country) { Carmen::Country.all.sample }
let(:params) { { country: country.name } }
before do
get :states, params: params
end
it { is_expected.to render_template(partial: 'profiles/_states') }
it 'renders the list of subregions' do
country.subregions.each do |subregion|
expect(response.body).to include CGI.escapeHTML subregion.name
end
end
end
end
......@@ -35,5 +35,9 @@ RSpec.describe ProfilesController, type: :routing do
expect(:delete => "/profiles/1").to route_to("profiles#destroy", :id => "1")
end
it "routes to #states" do
expect(:get => '/states').to route_to("profiles#states")
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment