This solution solves the problem of selecting a parent dropdown option which dynamically changes the options in another child dropdown (form_ui = :select) using simple AJAX (javascript onchange event).
------------------------------ Background: ------------------------------ To recap the problem, I working on a site that collect surveys. A survey has many questions and options. Conversely, a survey_question belongs_to a survey and a survey_option belongs_to a survey. ------------------------------ Problem: ------------------------------ The create and update view of AS wasn't user-friendly. For each survey, there are four questions. If there are 100 surveys, then there are 400 questions in a dropdown box - obviously a growing problem. My objective was to select a survey and have the survey_question dropdown dynamically populate with ONLY the questions that belong_to the survey. -----> [Survey] (parent) ----------> [Survey Question] (child) ------------------------------ Challenges: ------------------------------ Being a newbie to Ruby, Rails and ActiveScaffold. Finding concise, useful documentation and examples. ------------------------------ Solution: ------------------------------ Use ActiveScaffold Form Overrides (Thanks Sergio and Sri) http://www.activescaffold.com/docs/form-overrides Only updated two files: SurveyOptionHelper.rb - added override methods for survey and survey_question SurveyOptionController.rb - added method update survey_question_options ------------------------------ Code Sample: ------------------------------ ---------- ---------- [survey_option_helper.rb] ---------- ---------- module SurveyOptionHelper #Overrides the default survey_question column to filter the #survey_questions by survey def survey_question_form_column(record, field_name) #-------------------- #Handle Edit Form #-------------------- if( !params[:action].empty? && params[:action] == 'edit' ) #Find survey_id of record.survey_question_id survey_question = SurveyQuestion.find( :all, :conditions => ["id=?", record.survey_question_id], :limit => 1 ) #Find all questions associated w/that survey select_tag field_name, options_from_collection_for_select( Survey.find(:all, :conditions => ["survey_id=?", survey_question[0].survey_id], :joins => 'as s inner join survey_questions as sq on s.id = sq.survey_id'), "id", "question_text", record.survey_question_id.to_i ) #-------------------- #Handle New Form #-------------------- else #Find all questions associated w/that survey select_tag field_name, options_from_collection_for_select( SurveyQuestion.find(:all), "id", "question_text", nil ) end end #Overrides the default survey column to filter the #survey_questions by survey def survey_form_column(record, field_name) #-------------------- #Handle Edit Form #-------------------- if( !params[:action].empty? && params[:action] == 'edit' ) select_tag field_name, options_from_collection_for_select( Survey.find(:all), "id", "survey_name", record.survey_id.to_i), {:onChange => remote_function( :update => "survey_option", :url => {:action => 'update_survey_question_options'}, :with => "'survey_id='+this.value") } else select_tag field_name, options_from_collection_for_select( Survey.find(:all), "id", "survey_name", nil), {:onChange => remote_function( :update => "survey_option", :url => {:action => 'update_survey_question_options'}, :with => "'survey_id='+this.value") } end end end ---------- ---------- [survey_option_controller.rb] ---------- ---------- def update_survey_question_options if( (!params[:survey_id].blank?) && (!params[:survey_id].empty?) ) render :update do |page| page.replace_html "record[survey_question]", options_from_collection_for_select( SurveyQuestion.find(:all, :conditions => ["survey_id=?", params[:survey_id]]), "id", "question_text" ) end else render :update do |page| page.replace_html "record[survey_question]", nil end end end ---------- That's it! Hope this helps. Bill Screen ---------- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en -~----------~----~----~----~------~----~------~--~---
