# problem with dynamic selects using rjs

**URL:** <https://discuss.rubyonrails.org/t/problem-with-dynamic-selects-using-rjs/45946>\
**Category:** rubyonrails-talk\
**Created:** [September 10, 2009, 7:36am UTC](https://discuss.rubyonrails.org/t/problem-with-dynamic-selects-using-rjs/45946 "2009-09-10T07:36:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcos\_Muller](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Marcos\_Muller](https://discuss.rubyonrails.org/u/Marcos_Muller)\
**Post date:** [September 10, 2009, 7:36am UTC](https://discuss.rubyonrails.org/t/problem-with-dynamic-selects-using-rjs/45946/1 "2009-09-10T07:36:58Z")

</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!

---

<div class="post-metadata">

**Author:** ![Marcos\_Muller](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Marcos\_Muller](https://discuss.rubyonrails.org/u/Marcos_Muller)\
**Post date:** [September 11, 2009, 6:47am UTC](https://discuss.rubyonrails.org/t/problem-with-dynamic-selects-using-rjs/45946/2 "2009-09-11T06:47:23Z")

</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”)} %\>_

_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:

```ruby
try {
Element.update("condition_value_div", "<select id=\"condition_value\" name=\"condition_value\"><option value=\"private\">private</option>\n<option value=\"public\">public</option></select>\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</option>\\n<option value=\\\"public\\\">public</option></select>\\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\>
