Marcos Müller
2009-Sep-08  11:50 UTC
dynamic selects using rjs, page.replace_html and on_change
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...
*<short explanation>*
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>*
*<complete 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''%></div>
    <%= 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</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]*
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>
-- 
Marcos Müller Lopes Fonseca
Arquiteto da Informação - MAV Tecnologia
Gerente de projetos - UFMG Informática Jr.
Sistemas de Informação - UFMG
marcoscefet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / 87553132
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Marcos Müller
2009-Sep-08  18:37 UTC
Re: dynamic selects using rjs, page.replace_html and on_change
Hello, Did this email ge tto the mailing list?? Thanks 2009/9/8 Marcos Müller <marcoscefet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> 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... > > *<short explanation>* > 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>* > *<complete 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''%></div> > <%= 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</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]* > > 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> > > -- > Marcos Müller Lopes Fonseca > Arquiteto da Informação - MAV Tecnologia > Gerente de projetos - UFMG Informática Jr. > Sistemas de Informação - UFMG > marcoscefet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / 87553132 >-- Marcos Müller Lopes Fonseca Arquiteto da Informação - MAV Tecnologia Gerente de projetos - UFMG Informática Jr. Sistemas de Informação - UFMG marcoscefet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / 87553132 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---