Neil D'Souza
Neil D'Souza

Reputation: 246

collection_check_boxes in form_tag

I have a form tag which acts as a search form. I have a set of locations - params: name and id, which Im displaying as collection boxes in the form tag. The search tag displays people for each location who can then be added to the project. By choosing a location on the form tag, you can filter down people for just that particular location.

The search form works fine and Correctly gives me people at each location

Here is what is in the view

<%= collection_check_boxes(@locations, :location_ids, @locations.all, :id, :name) %><br> 

Here is the form tag it's enclosed in:

<%= form_tag interviewers_tenant_project_path(@project, tenant_id: @tenant.id), method: :get do %>

Locations are project specific, and I'm creating an instance variable to return all the locations valid for a project.

My problem is that after the form returns the data, the previously selected location check box is not ticked.

Here is my code in the controller

    @locations = Location.where("name in (?)", project_locations)

    @locations.each do |loc|
      puts "loc.name #{loc.name}, #{loc.id}"
      def loc.location_ids
        [1]
      end
    end


    def @locations.location_ids
      puts "ENTER @locations.location_ids "

      #location_values
      [1]
    end

the documentation says this:

collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)

"Returns check box tags for the collection of existing return values of method for object's class. The value returned from calling method on the instance object will be selected. If calling method returns nil, no selection is made."

So I've tried adding a method to the object (singleton method) and then when that didn't work, I tried adding a method to each active record instance. I have hardcoded the value [1] - which corresponds to the first location in my database. But I just cannot make the checkbox selected when the form is re-rendered after the search.

Im new to rails - about 2 months into my project - before that a month studying RoR, so all insights would be beneficial.

Thanks for your help and time

Upvotes: 2

Views: 1303

Answers (1)

Neil D&#39;Souza
Neil D&#39;Souza

Reputation: 246

I worked around this finally. I didn't want to re-write the controller. So I tried adjusting the names of the check boxes to the same names as those generated by the collection tag. I have also put 3 extra lines of code, starting with <% param ... which helped me debug the right value. I guess rails experts would call it an ugly hack ... ? Anyways, the check boxes work fine and I didnt have to touch the controller code.

    <% @locations.each do |loc| %>
        <%= params.has_key?(:location)  %>
        <%= params.has_key?(:location) && params[:location].has_key?(:location_ids) ? params[:location][:location_ids].to_s : "empty" %>
        <%= params.has_key?(:location) && params[:location].has_key?(:location_ids) && params[:location][:location_ids].include?(loc.id) %>
        <%= check_box_tag("location[location_ids][]" , loc.id, params.has_key?(:location) && params[:location].has_key?(:location_ids) && params[:location][:location_ids].include?(loc.id.to_s) ? loc.id : nil, id: "location_" + loc.id.to_s) %>
        <%= label_tag("location_"  + loc.id.to_s, loc.name) %>
    <% end  %>

Upvotes: 1

Related Questions