Forum: Ruby on Rails RJS, & mulitple Drop Down Boxes.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-05-09 17:56
Hello Again Rails Folk!

I'm trying to creat a page similar to what the person is doing here...

http://mudabone.com/aietc/?page_id=410

Instead of doing it the way he/she has, I decided to use RJS files, or
at least I tried.

Here's my view

<%= start_form_tag %>
<%= javascript_include_tag "prototype" %>

<p>
	Union:
	<div id="union">
		<select name="new_contract[union_id]" id="new_contract[union_id]">
			<option value="">Select Union</option>
   			<% @unions.each do |union| %>
      		<option value="<%= union.id %>">
         		<%= union.name %>
      		</option>
   			<% end %>
		</select>
	</div>
</p>
<p>
	Contract:
</p>
<p>
	<div id="contract_id_container">
		<select name="contracts" id="contracts">
			<option value="">Select Contract</option>
		</select>
	</div>
<p>
	Rate Group
</p>
<p>
	<div id="rate_group_id_container">
		<select name="rate_group" id="rate_group">
				<option value="">Select Rate Group</option>
		</select>
	</div>
</p>
<%= observe_field("new_contract[union_id]",
   :frequency => 0.25,
   :update => "contract",
   :url => { :action => :add_contract },
   :with => "'union_id='+value") %>

<%= observe_field("contract",
   :frequency => 0.25,
   :update => "rate_group",
   :url => { :action => :add_rate_group },
   :with => "'contract_id='+value") %>
<%= end_form_tag %>

and the two RJS files contiain somethign like this... for testing
purposes...

page.insert_html :bottom, 'contracts', '<option>Test</option>'

now, this works for the first change.  If I make a change in Union, the
Contract is updated no problem... however, when I make a change to the
Contract DropDown, I' not getting any changes to the third box ( Rate
Group ).

You would think there would be an OnChange event, rather than using
Observe_field.  It seems that the second Observe_field doesn't "see" the
contract element to watch it for changes.

I hope somone has a idea of what I'm trtying to do, and maybe some info
on what I'm doing wrong, or how I could do it better.

I can proabbly get it working the way the person in the linked article
did it, but I seem so close, I'm wondering what I'm doing wrong.

Cheers and thanks in advance,
Randal
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-09 19:00
Randal Santia wrote:
> Hello Again Rails Folk!

You can propbably do something like

<%= select  'new_contract', 'union_id',
            @unioins.collect {|u| [u.name, u.value]},
            :onchange => remote_function(
              :update => "contract",
              :url => { :action => :add_contract },
              :with => "'union_id='+this.value"
            ) %>
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-05-09 20:22
Alex Wayne wrote:
> Randal Santia wrote:
>> Hello Again Rails Folk!
>
> You can propbably do something like
>
> <%= select  'new_contract', 'union_id',
>             @unioins.collect {|u| [u.name, u.value]},
>             :onchange => remote_function(
>               :update => "contract",
>               :url => { :action => :add_contract },
>               :with => "'union_id='+this.value"
>             ) %>

I tried this out, and I don't seem to get anything to work...

It's probably because I have NO idea what's going on in he above code.
Does calling a "remote_function" call the RJS files?
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-05-09 22:26
Ok Tried somethign new...

View:

<p>
	<h3>Performer Category Rates</h3>
	Set Rates for Performer Categories according to Contracts, Unions and
Rate Groups.
</p>
<%= start_form_tag %>
<%= javascript_include_tag "prototype" %>

<p>
	Union:
	<div id="union">
		<select name="new_contract[union_id]" id="new_contract[union_id]"
onchange="<%= remote_function(
		              :url => { :action => :add_contract },
		              :with => "'union_id='+this.value"
		            )  %>">
			<option value="">Select Union</option>
		   	<% @unions.each do |union| %>
	     		<option value="<%= union.id %>">
	     			<%= union.name %>
	      		</option>
	   		<% end %>
		</select>
	</div>
</p>
<p>
	Contract:
</p>
<p>
	<div id="contract_container">

	</div>
</p>
<p>
	Rate Group:
</p>
<p>
	<div id="rate_group_container">

	</div>
</p>
<%= end_form_tag %>

Then I created two Template Files...

_contracts.rhtml
<select name="contracts" id="contracts" onchange="<%= remote_function(
              :url => { :action => :add_rate_group },
              :with => "'contract_id='+this.value"
            )  %>">
	<option value="">Select Contract</option>
   	<% @contracts.each do |contract| %>
 		<option value="<%= contract.id %>">
 			<%= contract.start_date %>
  		</option>
	<% end %>
</select>


and

_rate_groups

select name="rate_groups" id="rate_groups">
	<option value="">Select Rate Group</option>
   	<% @rate_roups.each do |rate_group| %>
 		<option value="<%= rate_group.id %>">
 			<%= rate_group.name %>
  		</option>
	<% end %>
</select>

and then two RJS files

add_contract.rjs
page.replace_html 'contract_container', :partial => 'contracts'

add_rate_group
page.replace_html 'rate_group_container', :partial => 'rate_groups'


The contract file updates fine when I select a union however the
rate_group template never shows up...  I've been fighting with this for
like two das, I'm pretty sure I'm just missing something small...

I hate being a n00b...

Any help would be much obliged.
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-05-09 23:13
Ok well, for anyone else who is trying this, the above way works...

Just make sure you do two things which help you out..

#1 - Watch teh WebRick server for errors, since they don't appear...

#2 - don't forget G's in the names of variables... :)

<% @rate_roups.each do |rate_group| %>

It works otherwise...
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 piggybox (Guest)
on 2006-05-10 03:07
Randal Santia wrote:

>
> now, this works for the first change.  If I make a change in Union, the
> Contract is updated no problem... however, when I make a change to the
> Contract DropDown, I' not getting any changes to the third box ( Rate
> Group ).
>
> You would think there would be an OnChange event, rather than using
> Observe_field.  It seems that the second Observe_field doesn't "see" the
> contract element to watch it for changes.
>
> I hope somone has a idea of what I'm trtying to do, and maybe some info
> on what I'm doing wrong, or how I could do it better.
>
> I can proabbly get it working the way the person in the linked article
> did it, but I seem so close, I'm wondering what I'm doing wrong.
>
> Cheers and thanks in advance,
> Randal


Just take out :frequency option from oberserve_field, then it will
trigger onchange without a fixed interval. :frequency is useful when you
want to monitor the input of a text field.

Good luck~
6f7363177ca4897a1e264e52914445fe?d=identicon&s=25 Marcia Almeida (Guest)
on 2006-05-25 19:35
Hello,

Try to put a :complete => "eval(request.responseText)" in your
remote_function declaration. Probabily, the code page.replace_html
'contract_container', :partial => 'contracts' is being converted in
javascript and this javascript is being received as text/html as
response. You have to evaluate javascript in order to work. you can try
another approach: create a method in your application_controller like
this:

class ApplicationController < ActionController::Base

  before_filter :init

  def init
    if request.xhr?
      @headers["Content-Type"] = "text/javascript; charset=utf-8"
    else
      @headers["Content-Type"] = "text/html; charset=utf-8"
    end
  end
...
end


This way, you don't have to use eval(...).

[]'s
Marcia


piggybox wrote:
> Randal Santia wrote:
>
>>
>> now, this works for the first change.  If I make a change in Union, the
>> Contract is updated no problem... however, when I make a change to the
>> Contract DropDown, I' not getting any changes to the third box ( Rate
>> Group ).
>>
>> You would think there would be an OnChange event, rather than using
>> Observe_field.  It seems that the second Observe_field doesn't "see" the
>> contract element to watch it for changes.
>>
>> I hope somone has a idea of what I'm trtying to do, and maybe some info
>> on what I'm doing wrong, or how I could do it better.
>>
>> I can proabbly get it working the way the person in the linked article
>> did it, but I seem so close, I'm wondering what I'm doing wrong.
>>
>> Cheers and thanks in advance,
>> Randal
>
>
> Just take out :frequency option from oberserve_field, then it will
> trigger onchange without a fixed interval. :frequency is useful when you
> want to monitor the input of a text field.
>
> Good luck~
This topic is locked and can not be replied to.