I am trying to update a list of radio options depending on the person that a user selects in a form.
_fields.html.erb:
<%= f.label :person %>
<%= f.select(:person_id, current_user.person_names) %>
<%= f.label :invoice_type %>
<%= radio_buttons_collection(f.object.invoice_types, f) %>
application_helper.rb:
def radio_buttons_collection(types, f) # works, but not with Ajax!
types_html = types.map do |type|
f.radio_button(:invoice_type, type)
end
safe_join(types_html)
end
projects_controller.rb:
def get_invoice_types
person = Person.find(params[:person_id])
@types = person.address_types
end
get_invoice_types.js.erb:
$('#project_invoice_type').html("<%= escape_javascript(radio_buttons_collection(@types, f)) %>");
application.js:
$("#project_person_id").change(function() {
$.ajax({
url: '/projects/get_invoice_types',
data: 'person_id=' + this.value,
dataType: 'script'
})
});
Everything works except that the radio options won't get updated when the person gets changed in the form.
Can anybody tell me how to do this?
Thanks for any help.
fis not nil?radio_buttons_collection(types, f)method doesn't get updated at all through Ajax. So I can't really test the output here.