Problem with dynamic selects using rjs

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
[email protected] / 87553132

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.

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/ 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]*

and i understand that they are trying to render the content at
http://localhost/rules/update_condition_value but the address that i’m
using
is 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. [email protected]

Posted via 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
[email protected] / 87553132