RE: [Rails] Re: dependent drop downs

I think you will want to read up on observers. In your initial view, create your 1st drop-down and an observer that watches that field. When a value is chosen in the drop-down, the observer will call a method in your controller (which in turn calls a partial view) and renders the 2nd drop down.

Cool guys, appreciate all the input! I'll let you know when it's done :slight_smile:

Stuart

<%= error_messages_for ‘infotantra’ %>

Category
Select Category <% @category.each do |category| %> > <%= category.category %> <% end %>

Sub-Category

       <option value=0>Select Sub-Category</option>
 </select>

Description

<%= text_area ‘infotantra’, ‘description’ , :rows=>“6”%>

<%= observe_field("infotantra[sub_category_id]
“,
:frequency => 0.25,
:update => "
sub_category”,
:url => { :action => :update_sub_category },#controller responsibility to do stuff
:with => “‘category_id=’+value”) %>#value for the action to use and compute stuff

in controller you might do…

def update_sub_category

  @sub_category = SubCategory.find(:all,:conditions => ["

category_id = ?" , @params["category_id
"]])#the value you sent
@html = “”
@html += “Sub-Category”

    @sub_category.each do |sub_category|
      @html += "<option value='#{sub_category.id}'>#{sub_category.sub_category}</option>"
    end
  
   @html += "</select>"

render(:layout => false)
end

hope this helps a little it worked for me

also hoping indentation remains the way it was

i made a mistake
my apologies

please correct

<%= observe_field(“infotantra[category_id]”,
:frequency => 0.25

If you leave out the frequency, the method is called when the field changes (onChange event), it’s of no real use to use frequency here. Also, if you want to do more than just update one dropdown, you can leave out the :update part too and then use an rjs template or render :update in your controller.

Best regards

Peter De Berdt

Question about 'frequency', does that not require any time of click or
submit, but works on the machine clock to check the field?

Stuart

Little confused here , sorry.
What is "infotantra" ?
For the first list I have something like this:

<select id="wage_id" name="wage[id]">
<option value="0">Please Select</option>
<option value="1">Hourly</option>
<option value="2">Annual</option>
</select>

Since it's only 2 options I just have it written out as opposed to a table.

Stuart

hi,
regarding frequency
Peter is right you dont need it…

with frequency it will check the field for a changed state at set intervals

infotantra is my table same as wage (in your case its not as you mentioned…)

regards
gaurav v bagga

Really sorry here because I'm getting an error just setting this up.
So maybe this shouldn't even go in this thread but I've already taken
too much bandwidth probably.

Right now I'm getting a no method error on a nil object.
I have two tables, pays and wages. (pays is the one they would choose
to decide on what is presented in the wages select)

I have both models set up.
class Pay < ActiveRecord::Base
has_many :wages
end

class Wage < ActiveRecord::Base
belongs_to :pay
end

Here is my first select -

  <select name="pay[wage_id]">
  <option value=0>Select Type</option>
   <% @wage.each do |wage| %>
       <option value="<%= wage.id %>"
         <%= ' selected' if wage.id == @pay.wage_id %>>
         <%= wage.wage %>
       </option>
   <% end %>
  </select></p>

Anyone see anything wrong.
The pays table is id and name
The wages table is id hran and name
the hran column is really the id that should match the id in pays.

Stuart

Peter De Berdt wrote:

Still a bit confused over what tables , whether it is one table, or
multiple and joined.
When you say infotantra[category_id] is this a join ? Meaning is
infotantra one table and category_id from another table ? Or
category_id is just a column/method from class infotantra ?

The way my table is set up is like this:
first table:
id name
1 hourly
2 annual
So here I've written out the select:

<%= @pays = Pay.find(:all, :order => "id").map { |p| [p.name, p.id] }
select(:pay, :name, @pays)%>

Then as an example (i'm not going to dump the entire table here, but
this is the second table:
id hran name
1 1 5.00
2 1 10.00
3 2 20,000
4 2 50,000

<div id="dollar">
<%= @wages = Wage.find(:all, :order => "id").map { |w| [w.name, w.id] }
select(:wage, :name, @wages, {}, {:disabled => true}) %></div>

Now the observer_field :

<%= observe_field([:pay.id],
           :frequency => 0.10,
           :update => "dollar",
           :url => {:action => :update_dollar},
           :with => "'pay.id='+value") %>

(I left the frequency in for now as there is no form_tag on the page
nor a submit button)
dollar is the DOM element. So far though I haven't written the
controller code and not sure how to. I'm still a newb (if you haven't
noticed) but I would be inclined to just do something like

if pay.id == 1
...code to make observer update field
else pay.id == 2

Sorry, if none of this makes sense.

Stuart

hi,

it aint that it does not makes sense thing is that
i myself i am new so i cannot say much

you are trying thats the best part

keep going on you will definately get through
i also banged my head to get it right

in my case also it wasnt that i got it in first try

regards
gaurav v bagga

Okay I installed KRJS , tested , did the sample index demo. Works fine!.

Now my problem:
On the page above you have :
1. # View
2. <%= select 'model','id' %>
3. <div id='select-b'>
4. <%= select 'model','category_id',
Model.find(@model.id).categories.map {|x| [x.name, x.id]} %>
5.</div>

Translating that to my models:

<%= select 'pay', 'id' %>
<div id='select-b'>
<%= select 'pay', 'wage_id', Pay.find(@pay.id).wages.map {|x|
[x.name, x.id] } %>
</div>

Throws an error:
Showing app/views/kr/index.rhtml where line #1 raised:
wrong number of arguments (2 for 3)

Extracted source (around line #1):

1: <%= select 'pay', 'id' %>
2: <div id='select-b'>
3: <%= select 'pay', 'wage_id', Pay.find(@pay.id).wages.map {|x|
4: [x.name, x.id] } %>

Stuart

It took me some time to make sense of all the various streams of help
here.. Figured I'd post a very standard example of how this works for
anyone who needs it.: