Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ajax for town selection #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//= require turbolinks
//= require leaflet
//= require_self
//= require registration

$(function () {
$(document).on('click', '.smooth-scroll', function (e) {
Expand Down
12 changes: 12 additions & 0 deletions app/assets/javascripts/registration.js.coffee
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*= require forms
*= require alerts
*= require leaflet
*= require registration
*= require_self
*/

Expand Down
7 changes: 7 additions & 0 deletions app/assets/stylesheets/registration.scss
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -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.none
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/registration_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RegistrationHelper
end
11 changes: 10 additions & 1 deletion app/views/schools/registrations/_school_fields.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
1 change: 1 addition & 0 deletions app/views/schools/registrations/update_towns.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$("#school_town_id").empty().append("<%= escape_javascript(render(:partial => @towns)) %>")
1 change: 1 addition & 0 deletions app/views/schools/towns/_town.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<option value="<%= town.id %>"><%= town.full_name %></option>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
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

as :speaker do
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)
Expand Down