0

我正在尝试根据personauser在表单中选择的选项更新单选选项列表。

_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

项目控制器.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)) %>");

应用程序.js:

$("#project_person_id").change(function() {
  $.ajax({
    url: '/projects/get_invoice_types',
    data: 'person_id=' + this.value,
    dataType: 'script'
  })
});

person一切正常,除了单选选项在表单中更改时不会更新。

谁能告诉我该怎么做?

谢谢你的帮助。

4

2 回答 2

1

我建议不要将表单对象传递给辅助方法

def radio_buttons_collection(types, invoice_type)
  types_html = types.map do |type|
    radio_button_tag("project[invoice_type]", type, type == invoice_type)
  end
  safe_join(types_html)
end

将 invoice_type 与类型一起传递给辅助方法。

在控制器定义

@invoice_type = person.invoice_type

get_invoice_types.js.erb:

$('#project_invoice_type').html("<%= escape_javascript(radio_buttons_collection(@types, @invoice_type)) %>");

_fields.html.erb:

<%= f.label :invoice_type %>
<div id = 'project_invoice_type'>
  <%= radio_buttons_collection(f.object.invoice_types, f.object.invoice_type) %>
</div>
于 2013-05-20T16:50:59.497 回答
0

您需要触发更改。在你的 get_invoice_types.js.erb 中试试这个

$('#project_invoice_type').html("<%= escape_javascript(radio_buttons_collection(@types, f)) %>");
$('#project_invoice_type').trigger('change');
于 2013-05-20T16:05:22.007 回答