TIA
Stuart
--
Posted via http://www.ruby-forum.com/.
Stuart
Stuart
As I'm reading through the AJAX stuff (in AWDWR) I'm wondering about a
few things
If I have this one element in my form where when user picks option
from another element is loaded - is this more of a javascript coding ,
like "onclick" or something that could be done with what's available
within Rails.
One idea is I'm having is to have that section of my form as a partial
that will get triggered in the controller to update. This make sense
?
Best regards
Peter De Berdt
-----Original Message-----
From: rubyonra...@googlegroups.com
[mailto:rubyonra...@googlegroups.com]On Behalf Of Dark Ambient
Sent: Thursday, September 07, 2006 8:17 AM
To: rubyonra...@googlegroups.com
Subject: [Rails] Re: dependent drop downs
As I'm reading through the AJAX stuff (in AWDWR) I'm wondering about a
few things
If I have this one element in my form where when user picks option
from another element is loaded - is this more of a javascript coding ,
like "onclick" or something that could be done with what's available
within Rails.
One idea is I'm having is to have that section of my form as a partial
that will get triggered in the controller to update. This make sense
?
Stuart
Stuart
<%= 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
Stuart
> <select name="infotantra[category_id]">
> <option value=0>Select Category</option>
> <% @category.each do |category| %>
> <option value="<%= category.id %>"
> <%= ' selected' if category.id == @infotantra.category_id %>>
> <%= category.category %>
> </option>
> <% end %>
> </select></p>
>
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
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
On 9/7/06, gaurav bagga <gaurav....@gmail.com> wrote:
<%= collection_select :pay, :wage_id, @wages, :id, :wage %>
HTH,
Nathan
-----Original Message-----
From: rubyonra...@googlegroups.com
[mailto:rubyonra...@googlegroups.com]On Behalf Of Dark Ambient
Sent: Thursday, September 07, 2006 1:03 PM
To: rubyonra...@googlegroups.com
Subject: [Rails] Re: dependent drop downs
Stuart
On 9/7/06, Nathan Leach <nathan...@phifer.com> wrote:
>
Yet another solution...
http://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/
_Kevin
www.sciwerks.com
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
On 9/7/06, gaurav bagga <gaurav....@gmail.com> 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 :
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
##############################
#[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 = "<select name='item2'>"
@html += "<option value='0'>Sub-Category</option>"
items[@params["item1"]].each do |sub_category|
@html += "<option
value='#{sub_category}'>#{sub_category}</option>"
end
@html += "</select>"
render_text(@html, :layout => false)
end
end
###############################
# views/test/main.rhtml
<%= javascript_include_tag "prototype" %>
<div id="item1_div">
<select name="item1">
<option value=0>Select Category</option>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
</div>
<div id="item2_div">
</div>
<%= 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