FreelanceWebDesigner.com
2006-Jul-27 06:34 UTC
[Rails] Update second pulldown menu based on first menu selection?
I know I''ve seen this somewhere on this board and just can''t search it anymore. I have three relational tables: clients, projects, and tasks. Currently I''m working on customizing the ''new/edit'' task _form. So far I have it listing clients in a pulldown menu so I can assign the new task to a client. I''d like to have a second pulldown select menu that would list the projects for the selected client though. Each time I select a different client in the first pulldown the second pulldown should automatically list the related projects for that client without having to reload the page. Since the second pulldown would get populated from a call to the client.projects table I''m guessing it has to be AJAX. Anybody seen RoR examples or tutorials on such tactics? Any help appreciated. Thanks, DAN -- Posted via http://www.ruby-forum.com/.
Peter De Berdt
2006-Jul-27 09:37 UTC
[Rails] Update second pulldown menu based on first menu selection?
On 27 Jul 2006, at 08:32, FreelanceWebDesigner.com wrote:> I know I''ve seen this somewhere on this board and just can''t search it > anymore. > > I have three relational tables: clients, projects, and tasks. > Currently > I''m working on customizing the ''new/edit'' task _form. So far I have it > listing clients in a pulldown menu so I can assign the new task to a > client. I''d like to have a second pulldown select menu that would list > the projects for the selected client though. Each time I select a > different client in the first pulldown the second pulldown should > automatically list the related projects for that client without having > to reload the page. > > Since the second pulldown would get populated from a call to the > client.projects table I''m guessing it has to be AJAX. Anybody seen RoR > examples or tutorials on such tactics? Any help appreciated.Quick and dirty (things can certainly be optimized so that the code is more DRY, but it should give you an idea): Main view: <p><label for="editrecord_customer_id">Customer : </label><%= select (:editrecord, :customer_id, @customers.collect {|p| [p.customername, p.customerid.to_i]}, {:prompt => "--- Customer list ---"},{:class => "required validate-number"})%></p> <p id="projectff"><%= render :partial => ''projectlist'' %></p> <p id="jobff"><%= render :partial => ''joblist'' %></p> <p id="taskff"><%= render :partial => ''tasklist'' %></p> <%= observe_field("editrecord_customer_id", :url => {:action => :findprojects})-%> _projectlist.rhtml <% if @projects %> <label for="editrecord_project_id">Project: </label><%= select (:editrecord, :project_id, [["--- Choose a project ---",""]]+ @projects.collect {|p| [p.name,p.id.to_i]}, {},{:class => "required validate-number"})%> <% else %> <label for="editrecord_project_id">Project: </label><%= select (:editrecord, :project_id, [["--- Choose a customer ---",""]], {}, {:class => "required validate-number"})%> <% end %> <%= observe_field("editrecord_project_id", :url => {:action => :findjobs})-%> _joblist.rhtml <% if @jobs %> <label for="editrecord_job_id">Job: </label><%= select (:editrecord, :job_id, [["--- Choose a job ---",""]]+@jobs.collect {| p| [p.name,p.id.to_i]}, {},{:class => "required validate-number"})%> <% else %> <label for="editrecord_job_id">Job : </label><%= select (:editrecord, :job_id, [["--- Choose a project ---",""]], {},{:class => "required validate-number"})%> <% end %> <%= observe_field("editrecord_job_id", :url => {:action => :findtasks})-%> _tasklist.rhtml <% if @tasks %> <label for="editrecord_task_id">Task: </label><%= select (:editrecord, :task_id, [["--- Choose a task ---",""]]+@tasks.collect {|p| [p.name,p.id.to_i]}, {},{:class => "required validate-number"})%> <% else %> <label for="editrecord_task_id">Task : </label><%= select (:editrecord, :task_id, [["--- Choose a job ---",""]], {},{:class => "required validate-number"})%> <% end %> In the controller : def findprojects @phrase = request.raw_post || request.query_string if !(@phrase == "") @projects= <put your find request here> @jobs=nil @tasks=nil end render :update do |page| page.replace_html :projectff, :partial => "projectlist" page.replace_html :jobff, :partial => "joblist" page.replace_html :taskff, :partial => "tasklist" end end def findjobs @phrase = request.raw_post || request.query_string if !(@phrase == "") @jobs = <put your find request here> @tasks=nil end render :update do |page| page.replace_html :jobff, :partial => "joblist" page.replace_html :taskff, :partial => "tasklist" end end def findtasks @phrase = request.raw_post || request.query_string if !(@phrase == "") @tasks = <put your find request here> end render :update do |page| page.replace_html :taskff, :partial => "tasklist" end end Hope this helps! Best regards Peter De Berdt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060727/c3675013/attachment.html