0

I use carmen-rails in my form to show country and sub-regions for each country, the first time when i select a country it update the list of subregions as well, then when i save these informations in database and go for the second time to above page and select another country it doesn't update the list of subregions !! then if reload page it does ! this is my js file in coffee-script format :

$ ->
  $('select#user_detail_attributes_country_code').change (event) ->

     select_wrapper = $('#user_detail_attributes_state_code')

     $('select', select_wrapper).attr('disabled', true)

     country_code = $(this).val()

     url = "/orders/subregion_options?parent_region=#{country_code}"
     select_wrapper.load(url)

note : I'm using rails 4, does rails 4 do some caching automatically and don't update the list ? or there is a problem with change method in jquery ? i can't find the problem here, any help please

my orders controller juste render a partial :

class OrdersController < ApplicationController

  def subregion_options
    render partial: 'orders/subregion_select'
  end

end

subregion partial code is :

div#order_state_code_wrapper
  - detail ||= params[:detail]
  - parent_region ||= params[:parent_region]
  - country = Carmen::Country.coded(parent_region) unless parent_region.nil?
  - if country.nil?
    %em Please select a country above
  - elsif country.subregions?
    = label_tag :user_detail_attributes_state_code, "State"
    = subregion_select_tag("user[detail_attributes][state_code]", detail.try(:state_code), parent_region)
  - else
    = label_tag :user_detail_attributes_state_code, "State"
    = text_field_tag("user[detail_attributes][state_code]", detail.try(:state_code))

Edit

i added alert("foo") inside jquery's change method, and when i select a country , sometimes it alert me "foo" and sometimes don't work !

mu is too short
  • 396,305
  • 64
  • 779
  • 743
medBouzid
  • 6,333
  • 8
  • 46
  • 72
  • what is the value returned by `"/orders/subregion_options?parent_region=#{country_code}"` – Arun P Johny Sep 25 '13 at 00:33
  • you need to sent `dddd – Arun P Johny Sep 25 '13 at 00:46
  • i don't think this is the problem, because i added alert("foo") inside change method of jquery, and when i select a country it don't show me the alert, i refresh again and it works, then i try again and it fails ... – medBouzid Sep 25 '13 at 00:51
  • can you share the related html – Arun P Johny Sep 25 '13 at 00:54
  • possible duplicate of [Rails 4 turbo-link prevents jQuery scripts from working](http://stackoverflow.com/questions/18769109/rails-4-turbo-link-prevents-jquery-scripts-from-working) – mu is too short Sep 25 '13 at 00:58
  • @ArunPJohny html use juste a helper form from carmen-rails there is no listed country, you can find an example of what it looks like here https://github.com/jim/carmen-rails, wait i show you my html – medBouzid Sep 25 '13 at 01:00
  • @muistooshort i think also that the problem is something like that, but can you tell me some steps to fix my problem please – medBouzid Sep 25 '13 at 01:04
  • @muistooshort your answer in the link isn't clear :( – medBouzid Sep 25 '13 at 01:07
  • `$(document).ready()` doesn't work with Turbolinks, `$ ->` is just another way of saying `$(document).ready()`, and Turbolinks is enabled by default in Rails4. You need to listen for different events (as noted in the linked answer) to initialize the page. – mu is too short Sep 25 '13 at 01:11
  • i think that problem is with change method not with $(document).ready(), i replace $ -> with $(document).on "page:change", -> but don't work – medBouzid Sep 25 '13 at 01:15

0 Answers0