0

personRails に ajax 関数があり、フォームで選択されたに基づいてオプション ハッシュを更新します。

# projects_controller.rb
def get_invoice_types
  person = Person.find(params[:person_id])  
  @types = person.address_types
end

# get_invoice_types.erb
$('#project_invoice_type').html("<%= escape_javascript(options_for_select(@types)) %>");

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

radio_buttonsの代わりに のリストを生成する簡単な方法はありoptions_for_selectますか?

もしそうなら、どうすればこれを行うことができますか?

助けてくれてありがとう。

これは、私が最終的に得たラジオボックスを生成するための関数です:

def collection_radio_buttons(types, f) # works!
  types_html = types.map do |type|
    content_tag :span, :class => "radio_option" do
      f.radio_button(:invoice_type, type) + localize_address_type(type)
    end
  end
  safe_join(types_html)
end

ただし、上記の Ajax 関数を介して更新することはできません。fこれは、関数に渡された変数が原因だと思いますか?

4

1 に答える 1

1

label_tagandを使用radio_button_tagして要素をループする open ヘルパーを記述します。

アップデート

このようなもの?

#Helper
  def radio_boxes(name, *items)
    content_tag :div do
      items.collect do |item|
        content_tag :label, item.first
        radio_button_tag(name, item.last) + label_tag("#{name}_#{item.last}", item.first)
      end.join("\n").html_safe
    end
  end

#View
<%= radio_boxes('countries', ['Lisbon', 1], ['Madrid', 2]) %>

#Output
<div>
  <input id="countries_1" name="countries" type="radio" value="1"><label for="countries_1">Lisbon</label>
  <input id="countries_2" name="countries" type="radio" value="2"><label for="countries_2">Madrid</label>
</div>
于 2013-03-04T20:34:15.630 に答える