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
Stuart
<%= error_messages_for ‘infotantra’ %>
Category
Select Category
<% @category.each do |category| %>
>
<%= category.category %>
<% end %>
Sub-Category
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}"
end
@html += "</select>"
render(:layout => false)
end
hope this helps a little it worked for me
also hoping indentation remains the way it was
Question about ‘frequency’, does that not require any time of click or
submit, but works on the machine clock to check the field?
Stuart
i made a mistake
my apologies
please correct
<%= observe_field(“infotantra[category_id]”,
:frequency => 0.25,
:update => “sub_category”,
:url => { :action => :update_sub_category },
:with => “‘category_id=’+value”) %>
<%end%>
please see the red field
regards
gaurav v bagga
On 9/7/06, gaurav bagga [email protected] wrote:
Select Category <% @category.each do |category| %> > <%= category.category %> <% end %>
Little confused here , sorry.
What is “infotantra” ?
For the first list I have something like this:
Since it’s only 2 options I just have it written out as opposed to a
table.
Stuart
On 07 Sep 2006, at 16:42, gaurav bagga wrote:
<%= 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
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
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 Type <% @wage.each do |wage| %> > <%= wage.wage %> <% end %>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
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
Peter De Berdt wrote:
If you leave out the frequency, the method is called when the field
border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; =
class=3D"Apple-style-span" style=3D"font-weight: bold; =
bold;“><SPAN style=3D"font-weight: bold;”><SPAN class=3D"Apple-style-span"=
leave out the frequency, the method is called when the field changes =–Apple-Mail-8–1052860537–
Yet another solution…
http://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/
_Kevin
www.sciwerks.com
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
On 9/7/06, _Kevin [email protected] wrote:
Yet another solution…
http://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/
_Kevin
www.sciwerks.com
Okay I installed KRJS , tested , did the sample index demo. Works
fine!.
Now my problem:
On the page above you have :
-
View
- <%= select ‘model’,‘id’ %>
-
- <%= select ‘model’,‘category_id’,
Model.find(@model.id).categories.map {|x| [x.name, x.id]} %>
5.
Translating that to my models:
<%= select ‘pay’, ‘id’ %>
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:
3: <%= select ‘pay’, ‘wage_id’, Pay.find(@pay.id).wages.map {|x|
4: [x.name, x.id] } %>
Stuart
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
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
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.:
##############################
#[test_controller.rb]
class TestController < ApplicationController
def main
end
def update_sub_category
puts @params["item1"]
items = {"1" => ["apple", "asphalt", "averatec"], "2" =>
[“bat”, “base”, “bowling”], “3” => [“cat”, “carol”]}
@html = “”
@html += “Sub-Category”
items[@params[“item1”]].each do |sub_category|
@html += “#{sub_category}”
end
@html += “”
render_text(@html, :layout => false)
end
end
###############################
views/test/main.rhtml
<%= javascript_include_tag “prototype” %>
<%= observe_field(
“item1”,
:frequency => 0.25,
:update => “item2_div”,
:url => { :controller => “test”, :action => :update_sub_category },
:with => “‘item1=’ + value”) %>
##########################################
sure there’s room for improvement but it gets a functional example