Filter select fields based on previous select fields values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Filter select fields based on previous select fields values

Guilherme Arruda

Hi everyone, I'm new at RoR and I'm having a trouble in my app. The problem consists on filter a select field named "Solution", based on the others select fields above it. The attached image shows an example of these fields.

Now, what the app do is to retrieve all information from BD abaout Area, Region, Associated, Solution and populate the select fields with these data. But the user wants that, when an area, a region and an associated is selected by the user, only the solutions about that associated in that region on that area should be shown. I know there is a way to do this with AJAX and JS, but I would like to do this with ruby only, in order to keep code clean and correct. Here is my controller code which populates the select fields:

def populate_selects
       @atendiment_area_users = AtendimentAreaUser.where(status: true, user_id: current_user.id)
       @atendiment_region_users = AtendimentRegionUser.where(status: true, user_id: current_user.id)
       @atendiment_region_associated = AtendimentRegionAssociated.where(status: true, entity_id: current_user.entity_id).where(atendiment_region_id: @atendiment_region_users.map(&:atendiment_region_id))
       @solution = Solution.where(entity_id: current_user.entity_id, status: true)
end

Below, the _form.html.erb code from view:

(...form_for...)
    <div class="field">
    <div class="row">
      <div class="col-xs-6">
        <%= f.select :atendiment_area_user_id , @atendiment_area_users.collect { |c| [ c.atendiment_area.name, c.id ] }, {:prompt=>"Area"}, { :class => 'form-control', :required => true } %>
      </div>
      <div class="col-xs-6">
        <%= f.select :atendiment_region_user_id , @atendiment_region_users.collect { |c| [ c.atendiment_region.name, c.id ] }, {:prompt=>"Region"}, { :class => 'form-control', :required => true } %>
      </div>
    </div>
  </div>

    <div class="field">
        <%= f.select :atendiment_region_associated_id , @atendiment_region_associated.collect { |c| [ c.associated.name, c.id ] }, {:prompt=>"Associated"}, { :class => 'form-control', :required => true } %>
      </div>
      <div class="field">
        <%= f.select :solution_id , @solution.collect { |c| [ c.solution.name, c.id ] }, {:prompt=>"Solution"}, { :class => 'form-control', :required => true } %>
      </div>

I've tried to use the AJAX remote: true on form_for, but it didn't worked.  If anyone can help, I apreciate. Thx for attention and sorry any bad english..

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/abdfa99a-14b6-4656-bb5b-e7a83a3cbe80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...