From 1b263d83597ab8215a5c061e2366241c6fe93d4d Mon Sep 17 00:00:00 2001 From: Mihail Ivanov Date: Tue, 23 Jun 2015 15:39:34 +0300 Subject: [PATCH 1/2] Added ajax for town selection --- app/assets/javascripts/application.js | 1 + app/assets/javascripts/registration.js.coffee | 12 ++++++++++ app/assets/stylesheets/application.css.scss | 1 + app/assets/stylesheets/registration.scss | 7 ++++++ app/controllers/registrations_controller.rb | 23 +++++++++++++++++++ app/helpers/registration_helper.rb | 2 ++ .../registrations/_school_fields.html.erb | 11 ++++++++- .../registrations/update_towns.js.coffee | 1 + app/views/schools/towns/_town.html.erb | 1 + config/routes.rb | 3 ++- 10 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/registration.js.coffee create mode 100644 app/assets/stylesheets/registration.scss create mode 100644 app/controllers/registrations_controller.rb create mode 100644 app/helpers/registration_helper.rb create mode 100644 app/views/schools/registrations/update_towns.js.coffee create mode 100644 app/views/schools/towns/_town.html.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 131a13d..0cbe0c9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -15,6 +15,7 @@ //= require turbolinks //= require leaflet //= require_self +//= require registration $(function () { $(document).on('click', '.smooth-scroll', function (e) { diff --git a/app/assets/javascripts/registration.js.coffee b/app/assets/javascripts/registration.js.coffee new file mode 100644 index 0000000..93b7acd --- /dev/null +++ b/app/assets/javascripts/registration.js.coffee @@ -0,0 +1,12 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ + +$ -> + $(document).on 'change', '#municipality_id', (evt) -> + $.ajax 'update_towns', + type: 'GET' + dataType: 'script' + data: { + municipality_id: $("#municipality_id option:selected").val() + } diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 8e34148..07de55f 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -17,6 +17,7 @@ *= require forms *= require alerts *= require leaflet + *= require registration *= require_self */ diff --git a/app/assets/stylesheets/registration.scss b/app/assets/stylesheets/registration.scss new file mode 100644 index 0000000..968f5dc --- /dev/null +++ b/app/assets/stylesheets/registration.scss @@ -0,0 +1,7 @@ +// Place all the styles related to the Registration controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ + +select { + min-width: 450px; +} diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 0000000..ab618b5 --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,23 @@ +class RegistrationsController < Devise::RegistrationsController + def new + @municipalities = Municipality.order('name ASC') + set_towns + super + end + + def update_towns + set_towns + respond_to do |format| + format.js + end + end + + private + def set_towns + if params[:municipality_id].present? + @towns = Town.where('municipality_id = ?', params[:municipality_id]).order('name ASC').includes(municipality: :state) + else + @towns = Town.where('1 != 1') + end + end +end diff --git a/app/helpers/registration_helper.rb b/app/helpers/registration_helper.rb new file mode 100644 index 0000000..e0bf900 --- /dev/null +++ b/app/helpers/registration_helper.rb @@ -0,0 +1,2 @@ +module RegistrationHelper +end diff --git a/app/views/schools/registrations/_school_fields.html.erb b/app/views/schools/registrations/_school_fields.html.erb index 47981bd..ced2a84 100644 --- a/app/views/schools/registrations/_school_fields.html.erb +++ b/app/views/schools/registrations/_school_fields.html.erb @@ -1,3 +1,12 @@ <%= form.input :name %> -<%= form.association :town, collection: Town.order('name ASC').includes(municipality: :state), label_method: :full_name %> + +<%= form.input :town, + input_html: { id: 'municipality_id' }, + collection: @municipalities, + required: true, + label_method: :name, + label: I18n.t('activerecord.models.municipality'), + :include_blank => true %> + +<%= form.association :town, collection: @towns, label_method: :full_name %> <%= form.input :address %> diff --git a/app/views/schools/registrations/update_towns.js.coffee b/app/views/schools/registrations/update_towns.js.coffee new file mode 100644 index 0000000..485d233 --- /dev/null +++ b/app/views/schools/registrations/update_towns.js.coffee @@ -0,0 +1 @@ +$("#school_town_id").empty().append("<%= escape_javascript(render(:partial => @towns)) %>") \ No newline at end of file diff --git a/app/views/schools/towns/_town.html.erb b/app/views/schools/towns/_town.html.erb new file mode 100644 index 0000000..372dcc4 --- /dev/null +++ b/app/views/schools/towns/_town.html.erb @@ -0,0 +1 @@ + diff --git a/config/routes.rb b/config/routes.rb index 11dcedd..326ba3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,7 @@ resources :frequently_asked_questions, path: 'frequently-asked-questions' as :school do + get 'schools/update_towns', to: 'registrations#update_towns', as: :update_towns get 'schools/profile', to: 'devise/registrations#edit', as: :school_root end @@ -23,7 +24,7 @@ get 'speakers/profile', to: 'devise/registrations#edit', as: :speaker_root end - devise_for :schools + devise_for :schools, :controllers => {:registrations => "registrations"} devise_for :speakers devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) From 872bfc45deeaedf7f8ead03cb0659ef8f4ce3fe2 Mon Sep 17 00:00:00 2001 From: Mihail Ivanov Date: Thu, 25 Jun 2015 19:45:07 +0300 Subject: [PATCH 2/2] Fixed Town empty relation --- app/controllers/registrations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index ab618b5..9584c13 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -17,7 +17,7 @@ def set_towns if params[:municipality_id].present? @towns = Town.where('municipality_id = ?', params[:municipality_id]).order('name ASC').includes(municipality: :state) else - @towns = Town.where('1 != 1') + @towns = Town.none end end end