# Dynamic selects using rjs, page.replace\_html and on\_change

**URL:** <https://www.ruby-forum.com/t/dynamic-selects-using-rjs-page-replace-html-and-on-change/175138>\
**Category:** Rails\
**Created:** [September 8, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/dynamic-selects-using-rjs-page-replace-html-and-on-change/175138 "2009-09-08T19:52:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcos\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/96bed5/32.png) [@Marcos\_MSSS\_ller](https://www.ruby-forum.com/u/Marcos_MSSS_ller)\
**Post date:** [September 8, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/dynamic-selects-using-rjs-page-replace-html-and-on-change/175138/1 "2009-09-08T19:52:18Z")

</div>

Hello everybody,

I’m having a “simple” problem but is taking me a long time to understand  
and  
i’ll try to do a short and a full explanation about the problem…

I’m quite beginner at rails and i’m facing a weird problem to make a two  
select boxes working dynamic. I have a table called conditions and this  
table has condition\_type and condition\_value. I want to put in the first  
select all the condition types and as an user select a type the  
condition  
values for that type (this means all the rows that have  
condition\_type=selected) will be show in a second select.

So far i’m trying different approachs like railcast #88 or  
[http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails/](http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails/) but  
i  
dont know what is happening.

When i select the condition\_type the on\_change method is called and then  
the  
controller is called and make the correct search on the database and i  
can  
get the correct response at firebug but the second select is not  
rendered  
with the new data.

_\</short explanation\>_

So far, my code is:

\*rules\_controller.rb  
def new  
@rule = Rule.new  
@condition\_type = Condition.find(:all, :select =\> ‘DISTINCT  
condition\_type’)  
@condition\_value = Condition.find(:all, :select =\> ‘DISTINCT  
condition\_value’)  
end

def update\_condition\_value  
@condition\_value = Condition.find(:all, :conditions =\> {  
:condition\_type =\> params[:condition\_type]})  
render :action =\> “update\_condition\_value.js.rjs”  
end

\_form\_new.html.erb  
\<% form\_for @rule do |rule\_form| %\>  
Name: \<%= rule\_form.text\_field :rule\_name %\>  
Condition type: \<%= select\_tag ‘condition\_type[]’,

options\_from\_collection\_for\_select(@condition\_type, ‘condition\_type’,  
‘condition\_type’),  
{:onchange =\>  
remote\_function(:url =\>  
{:action =\>  
“update\_condition\_value”}, :with =\> “‘condition\_type=’+value”)} %\>

```
<div id="condition_value_div">Condition value: <%= render :partial 

```

=\>  
‘condition\_value’%\>  
\<%= rule\_form.submit “Submit” %\>  
\<%end%\>

\_condition\_value.html.erb  
\<%= select\_tag ‘condition\_value’,  
options\_from\_collection\_for\_select(@condition\_value, ‘condition\_value’,  
‘condition\_value’)%\>

update\_condition\_value.js.rjs  
page.replace\_html ‘condition\_value\_div’, :partial =\>  
‘condition\_value’e’)\*

The problem that i’m facing is that when i select some condition\_type  
the  
on\_change is activated and then they call the  
update\_condition\_value.js.rjs  
and i can see from the firebug that i receive an response like:

try {  
Element.update(“condition\_value\_div”, “\<select id="condition\_value"  
name="condition\_value"\>\<option  
value="private"\>private\n\<option  
value="public"\>public\n\n”);  
} catch (e) { alert(‘RJS error:\n\n’ + e.toString());  
alert(‘Element.update("condition\_value\_div", "\<select  
id=\"condition\_value\" name=\"condition\_value\"\>\<option  
value=\"private\"\>private\n\<option  
value=\"public\"\>public\n\n");’); throw e }

as it should be… This means that they went to the controller and make  
the  
correct call from the database as i can see at the log at netbeans. But  
the  
problem is that the page is not rendered or any changes occoru with the  
second select.

So, i can say that every step is working until the call from the  
controller  
at update\_condition\_value but looks like i have an mismatch or error at  
render :action =\> “update\_condition\_value.js.rjs” call.

I think maybe could be a problem with my routes cause when i see the log  
after i select something at the first select i get this:

- Processing RulesController#update\_condition\_value to json (for  
127.0.0.1  
at 2009-09-08 14:43:18) [POST]  
Parameters: {“format”=\>“json”, “condition\_type”=\>“group”,  
“action”=\>“update\_condition\_value”, “controller”=\>“rules”}  
[4;36;1mCondition Load (0.0ms)[0m [0;1mSELECT \* FROM `conditions`  
WHERE  
(`conditions`.`condition_type` = ‘group’)  
Rendering rules/update\_condition\_value  
[4;35;1mCondition Columns (0.4ms)[0m [0mSHOW FIELDS FROM  
`conditions`[0m  
Rendered rules/\_condition\_value (1.8ms)  
Completed in 89ms (View: 2, DB: 5) | 200 OK [  
[http://localhost/rules/update\_condition\_value](http://localhost/rules/update_condition_value)]\*

and i understand that they are trying to render the content at  
[http://localhost/rules/update\_condition\_value](http://localhost/rules/update_condition_value) but the address that i’m  
using  
is [http://localhost:3000/coreui/privacy/1/rules/new](http://localhost:3000/coreui/privacy/1/rules/new)  
as u can see at the routes file:  
\*  
map.resources :rules, :path\_prefix =\> ‘/coreui/privacy/:user\_id’\*

I dont know what else i should do to make this work and any help or tips  
will be welcome!

Thanks in advance…

\</complete explanation\>

–  
Marcos MÃ¼ller Lopes Fonseca  
Arquiteto da InformaÃ§Ã£o - MAV Tecnologia  
Gerente de projetos - UFMG InformÃ¡tica Jr.  
Sistemas de InformaÃ§Ã£o - UFMG  
removed\_email\_address@domain.invalid / 87553132

---

<div class="post-metadata">

**Author:** ![Marcos\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/96bed5/32.png) [@Marcos\_MSSS\_ller](https://www.ruby-forum.com/u/Marcos_MSSS_ller)\
**Post date:** [September 8, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/dynamic-selects-using-rjs-page-replace-html-and-on-change/175138/2 "2009-09-08T20:39:44Z")

</div>

Hello,  
Did this email ge tto the mailing list??  
Thanks

2009/9/8 Marcos MÃ¼ller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> values for that type (this means all the rows that have
> 
> ```
> @condition_value = Condition.find(:all, :select => 'DISTINCT
> <% form_for @rule do |rule_form| %>
> <div id="condition_value_div">Condition value: <%= render :partial =>
> page.replace_html 'condition_value_div', :partial =>
> 
> ```
> 
> after i select something at the first select i get this:  
> Rendered rules/\_condition\_value (1.8ms)  
> I dont know what else i should do to make this work and any help or tips  
> Sistemas de InformaÃ§Ã£o - UFMG  
> removed\_email\_address@domain.invalid / 87553132

–  
Marcos MÃ¼ller Lopes Fonseca  
Arquiteto da InformaÃ§Ã£o - MAV Tecnologia  
Gerente de projetos - UFMG InformÃ¡tica Jr.  
Sistemas de InformaÃ§Ã£o - UFMG  
removed\_email\_address@domain.invalid / 87553132
