I am quite newbie to rails and stuck with a problem. In my application I have a Ajax.updater call to update counties list in a select drop down whenever a city name is selected. I need to do this in many place in my application. Currently the Ajax is working fine. But the get_counties action is in the controller. So I am using this action in all controllers that I need. However, it would be nice if I could move this to a County or City model or application_helper.rb . Calling this method from application_helper is not working as rails/ajax still looks for this action in the controller. How can I deal with this? And in general what will be the best place to add this method. Below is my code: Controller action: -------------------------- def get_counties @county = {} @counties = {} @fake_county_name = ''(city)'' if not params[:id].blank? index = params[:id] @counties = County.find(:all, :conditions=> ["city_id = ? AND name != ?", index, @fake_county_name ], :order=>"name") end render :partial => ''county'', :locals=>{ "counties"=>@counties, "county"=>@counties } end -------------------------- -------------------------- -------------------------- View partial: -------------------------- <table> <tr> <td class="selectcity"> <label for=''city_id''>Select city:* </label></td><td class="cityselect"> <span id="selectcity"> <select id="city_id" name="user[city_id]" onchange="new Ajax.Updater(''selectcounty'', ''get_counties'', {asychronous:true, evalScripts:true, parameters:''id=''+value})"> <option value=""></option> <%options_from_collection_for_select(@cities,"id","name",@city[:id]) %> </select> </span> </td> </tr> <tr> <td class="selectcounty"> <label for=''county_id''>Select county:* </label></td><td class="countyselect"> <span id="selectcounty"> <select id="user_county_id" name="user[county_id]"> <option value=""></option> <%= options_from_collection_for_select(@counties, "id", "name", @county[:id]) %> </select> </span> </td> </tr> </table> ------------------------- ------------------------- _section.rhtml <select id="user_section_id" name="user[section_id]"> <option value=""></option> <%= options_from_collection_for_select(@secs, "id", "name", @sec[:id]) %> </select> ------------------------- Cheers, CS. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-28 23:56 UTC
Re: Ajax.updater to call app_helper or model method..
On Feb 28, 11:03 pm, Carlos Santana <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I am quite newbie to rails and stuck with a problem. > > In my application I have a Ajax.updater call to update counties list in > a select drop down whenever a city name is selected. I need to do this > in many place in my application. Currently the Ajax is working fine. But > the get_counties action is in the controller. So I am using this action > in all controllers that I need. However, it would be nice if I could > move this to a County or City model or application_helper.rb . Calling > this method from application_helper is not working as rails/ajax still > looks for this action in the controller. How can I deal with this? And > in general what will be the best place to add this method. >The more conventional way to do this would to have a single get_counties action in the Cities controller (or expose counties as a nested resource of cities, but you might want to leave that for another day). Your if you change your ajax updater to make a request to /some_controller/some_action then all of the pages using this partial will make their ajax requests to the same place Fred> Below is my code: > > Controller action: > -------------------------- > def get_counties > @county = {} > @counties = {} > @fake_county_name = ''(city)'' > if not params[:id].blank? > index = params[:id] > @counties = County.find(:all, :conditions=> ["city_id = ? AND name > != ?", index, @fake_county_name ], :order=>"name") > end > render :partial => ''county'', :locals=>{ "counties"=>@counties, > "county"=>@counties } > end > > -------------------------- > > -------------------------- > -------------------------- > > View partial: > -------------------------- > <table> > <tr> > <td class="selectcity"> > <label for=''city_id''>Select city:* </label></td><td class="cityselect"> > <span id="selectcity"> > <select id="city_id" name="user[city_id]" onchange="new > Ajax.Updater(''selectcounty'', ''get_counties'', > {asychronous:true, evalScripts:true, > parameters:''id=''+value})"> > <option value=""></option> > <%> options_from_collection_for_select(@cities,"id","name",@city[:id]) %> > </select> > </span> > </td> > </tr> > <tr> > <td class="selectcounty"> > <label for=''county_id''>Select county:* </label></td><td > class="countyselect"> > <span id="selectcounty"> > <select id="user_county_id" name="user[county_id]"> > <option value=""></option> > <%= options_from_collection_for_select(@counties, "id", "name", > @county[:id]) %> > </select> > </span> > </td> > </tr> > > </table> > > ------------------------- > > ------------------------- > _section.rhtml > <select id="user_section_id" name="user[section_id]"> > <option value=""></option> > <%= options_from_collection_for_select(@secs, "id", "name", > @sec[:id]) %> > </select> > ------------------------- > > Cheers, > CS. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Carlos Santana
2009-Mar-01 00:48 UTC
Re: Ajax.updater to call app_helper or model method..
Thanks Fred.. I don''t have counties or cities controller right now. The get_counties/sections method is placed in the other controllers (users and exhibitions). I wanted to do this application RESTful way, however I haven''t been successful in doing so. Your suggestion of making counties as a nested resource of cities was my initial thought. (And also user belongs to a county. ) However, when I tried to do RESTful the ''usual CRUD actions'' (list, add, show, update, delete) for the user_controller worked but get_counties action did not work. The error being in Ajax.updater method as it uses POST and REST allowed only get, put, and delete. Can someone help me in understanding this? 1. Any suggestion for placing get_sections method? 2. And how to use actions in RESTful way? Many tutorials point to list, add, show, update, delete example.. But how can we do it for other actions. (In my case essentially it will be a list of counties pertaining to particular city.. But I don''t know how to do this.. Any help appreciated.. Thanks, CS. Frederick Cheung wrote:> On Feb 28, 11:03�pm, Carlos Santana <rails-mailing-l...@andreas-s.net> > wrote: >> in general what will be the best place to add this method. >> > > The more conventional way to do this would to have a single > get_counties action in the Cities controller (or expose counties as a > nested resource of cities, but you might want to leave that for > another day). Your if you change your ajax updater to make a request > to /some_controller/some_action then all of the pages using this > partial will make their ajax requests to the same place > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-01 02:00 UTC
Re: Ajax.updater to call app_helper or model method..
On Mar 1, 12:48 am, Carlos Santana <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks Fred.. > > I don''t have counties or cities controller right now. The > get_counties/sections method is placed in the other controllers (users > and exhibitions). > > I wanted to do this application RESTful way, however I haven''t been > successful in doing so. Your suggestion of making counties as a nested > resource of cities was my initial thought. (And also user belongs to a > county. ) However, when I tried to do RESTful the ''usual CRUD actions'' > (list, add, show, update, delete) for the user_controller worked but > get_counties action did not work. The error being in Ajax.updater method > as it uses POST and REST allowed only get, put, and delete. >You can use any method with an Ajax.Updater ( check the prototype api docs).> Can someone help me in understanding this? > 1. Any suggestion for placing get_sections method? > 2. And how to use actions in RESTful way? Many tutorials point to list, > add, show, update, delete example.. But how can we do it for other > actions. (In my case essentially it will be a list of counties > pertaining to particular city.. But I don''t know how to do this.. Any > help appreciated.. >You can have arbitrary actions on both members of a collection or the collection itself. I don''t think you need this here though. I would recommend you read through the guide on routing ( http://guides.rubyonrails.org/routing_outside_in.html ) Fred> Thanks, > CS. > > Frederick Cheung wrote: > > On Feb 28, 11:03 pm, Carlos Santana <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> in general what will be the best place to add this method. > > > The more conventional way to do this would to have a single > > get_counties action in the Cities controller (or expose counties as a > > nested resource of cities, but you might want to leave that for > > another day). Your if you change your ajax updater to make a request > > to /some_controller/some_action then all of the pages using this > > partial will make their ajax requests to the same place > > > Fred > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Carlos Santana
2009-Mar-01 02:40 UTC
Re: Ajax.updater to call app_helper or model method..
Frederick Cheung wrote:> On Mar 1, 12:48�am, Carlos Santana <rails-mailing-l...@andreas-s.net> > wrote: >> (list, add, show, update, delete) for the user_controller worked but >> get_counties action did not work. The error being in Ajax.updater method >> as it uses POST and REST allowed only get, put, and delete. >> > You can use any method with an Ajax.Updater ( check the prototype api > docs).If I use get method then it does not update the select dropdown (which is what it is intended to do).> >> Can someone help me in understanding this? >> 1. Any suggestion for placing get_sections method? >> 2. And how to use actions in RESTful way? Many tutorials point to list, >> add, show, update, delete example.. But how can we do it for other >> actions. (In my case essentially it will be a list of counties >> pertaining to particular city.. �But I don''t know how to do this.. Any >> help appreciated.. >> > You can have arbitrary actions on both members of a collection or the > collection itself. I don''t think you need this here though. I would > recommend you read through the guide on routing ( > http://guides.rubyonrails.org/routing_outside_in.html ) > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-01 02:49 UTC
Re: Ajax.updater to call app_helper or model method..
On Mar 1, 2:40 am, Carlos Santana <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > On Mar 1, 12:48 am, Carlos Santana <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> (list, add, show, update, delete) for the user_controller worked but > >> get_counties action did not work. The error being in Ajax.updater method > >> as it uses POST and REST allowed only get, put, and delete. > > > You can use any method with an Ajax.Updater ( check the prototype api > > docs). > > If I use get method then it does not update the select dropdown (which > is what it is intended to do). > >Then you must be doing something funny somewhere as (apart from routing) whether it''s a get or a post is pretty much irrelevant (and semantically get is the correct method to be using here). Fred> > >> Can someone help me in understanding this? > >> 1. Any suggestion for placing get_sections method? > >> 2. And how to use actions in RESTful way? Many tutorials point to list, > >> add, show, update, delete example.. But how can we do it for other > >> actions. (In my case essentially it will be a list of counties > >> pertaining to particular city.. But I don''t know how to do this.. Any > >> help appreciated.. > > > You can have arbitrary actions on both members of a collection or the > > collection itself. I don''t think you need this here though. I would > > recommend you read through the guide on routing ( > >http://guides.rubyonrails.org/routing_outside_in.html) > > > Fred > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Carlos Santana
2009-Mar-01 09:13 UTC
Re: Ajax.updater to call app_helper or model method..
It works fine until I start making my application RESTful way... Any clues? How can I debug this issue? Frederick Cheung wrote:> On Mar 1, 2:40�am, Carlos Santana <rails-mailing-l...@andreas-s.net> > wrote: >> If I use get method then it does not update the select dropdown (which >> is what it is intended to do). >> >> > Then you must be doing something funny somewhere as (apart from > routing) whether it''s a get or a post is pretty much irrelevant (and > semantically get is the correct method to be using here). > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---