开发者

Ruby on Rails select tags from hash

开发者 https://www.devze.com 2023-02-22 04:44 出处:网络
I have a hash stored as a constant defined in application.rb It looks a bit like this: ITEMS = {开发者_Go百科 \"Item 1\" => [\'1 - sdfsdf\', \'2 - sdlfksdf\'], \"Item 2\" => [\'1 - lkfsdf\', 2

I have a hash stored as a constant defined in application.rb

It looks a bit like this:

ITEMS = {开发者_Go百科 "Item 1" => ['1 - sdfsdf', '2 - sdlfksdf'], "Item 2" => ['1 - lkfsdf', 2- dkfdjk']}

What I would like from this is one combo box with the options:

Item 1

Item 2

and a second combo box with the items in the array in the hash depending on the selection of the first.

Is there an easy way to do this with rails 3 (I am using JQuery)

Thanks


You could create the first box with the following:

<%= f.select :var_name, ITEMS.collect { |key, value| [key, key] } %>

Then, add an observer

<%= observe_field 'element_var_name',
      :url => { :action => "another_action_here" },
      :update => "div_tag_to_update",
      :with => "'selected='+ escape($('element_var_name').value)" %>

Be sure to adjust element_var_name and the action to your situation. The another_action_here action should render a view like this:

def call_ids_by_type
  @element_list = ITEMS[param[:selected].collect { |value| [value, value] }
  render :layout => false
end

The associated view should only contain the select field you want to add. Haven't tried it exactly, but I think this should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消