0

http://railscasts.com/episodes/88-dynamic-select-menus-revised?view=asciicast
の場合、どうにかうまくいきません。コードの何が問題になっていますか?

ビュー/登録/edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<% resource.build_user_profile if resource.user_profile.nil? %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :class => 'form-horizontal' }) do |f| %>

   <%= devise_error_messages! %>

   <%= f.fields_for :user_profile do |profile_form| %>

       <%= profile_form.label :country_id %><br />
       <%= profile_form.collection_select :country_id, Country.all, :id, :name, include_blank: true %>

       <%= profile_form.label :prefecture_id, "State or Prefecture" %><br />
       <%= profile_form.grouped_collection_select :prefecture_id, Country.all, :prefectures, :name, :id, :name, include_blank: true %>   

   <% end %> 

    <br />

    <div class="control-group">
        <div class="controls">
            <%= f.submit 'Update', :class => 'btn btn-primary' %>
        </div>
    </div>

<% end %>

user_profiles.js.coffee

jQuery ->
  $('#user_profile_prefecture_id').parent().hide()
  states = $('#user_profile_prefecture_id').html()
  console.log(prefectures)
  $('#user_profile_country_id').change ->
    country = $('#user_profile_country_id :selected').text()
    escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(prefectures).filter("optgroup[label=#{escaped_country}]").html()
    console.log(options)
    if options
      $('#user_profile_prefecture_id').html(options)
      $('#user_profile_prefecture_id').parent().show()      
    else
      $('#user_profile_prefecture_id').empty()
      $('#user_profile_prefecture_id').parent().hide()
4

1 に答える 1

2

いずれにせよ、あなたが合格するとき:

<%= f.fields_for :user_profile ... %>

親フォーム ビルダーにオブジェクトがある場合、フィールドの属性name=およびid=を「#user_profile...」にすることはできません。ID を正確に判断するには、ソース コード (HTML 応答) を参照してください。

于 2013-01-06T01:22:28.307 に答える