# Problem with dynamic selects using rjs

**URL:** <https://www.ruby-forum.com/t/problem-with-dynamic-selects-using-rjs/175261>\
**Category:** Rails\
**Created:** [September 10, 2009, 9:39am UTC](https://www.ruby-forum.com/t/problem-with-dynamic-selects-using-rjs/175261 "2009-09-10T09:39:05Z")\
**Posts on this page:** 3\
**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 10, 2009, 9:39am UTC](https://www.ruby-forum.com/t/problem-with-dynamic-selects-using-rjs/175261/1 "2009-09-10T09:39:05Z")

</div>

Hello everybody,

This is a simple problem and i found a lot of resources on the internet  
but  
i try for different ways and none of them worked yet. I have the normal  
problem with the “states and cities” selects.

But the problem is that when i select the state 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.

I think i have some problems with routes or something like this cause  
the  
page is not rendered correct but looks like the response is for another  
url.  
i’m dont know what could be wrong.

Any help will be appreciate!

–  
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 10, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/problem-with-dynamic-selects-using-rjs/175261/2 "2009-09-10T19:47:26Z")

</div>

Marcos,

You need to provide some code for the controller, the model, and the  
view where you are having the issue. Without any code, no one can  
adequately respond.

Thanks.

---

<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 11, 2009, 8:49am UTC](https://www.ruby-forum.com/t/problem-with-dynamic-selects-using-rjs/175261/3 "2009-09-11T08:49:29Z")

</div>

Hello again,

I’m going to try to explain better now…

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\>

2009/9/10 Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
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
