Search code examples
ruby-on-railsrubyformscheckboxlist

Form_for with render list of check_box


I have functionality search above tags. I want to render all tags with check_box in form_for that I will filter those tags and render all items again with new parameters.

= form_for @tags do |f|
  - @tags.each do |tag|
    f.check_box(#{tag}, "yes")
    f.label_tag "#{tag}"
  f.submit 'Filter'
end

Solution

  • I don't know what you are asking because your question is not much clear, but trying to understand your question. If I understood your question then you need to add a filter with tags on the search page, right? If yes then try the following steps.

    Create a form for search tag like

    <%= form_tag search_path, method: :get do %>
       <% for tag in Model.order("name") %> #=> Model means which stored your tags like "Tag"
           <%= check_box_tag("#{tag.name}[]", "#{tag.name}") %> tag.name
           OR
           <%= check_box_tag("tags[]", "#{tag.name}") %> tag.name
       <% end %>
       <%= submit_tag "Filter" %>
    <% end %>
    

    Or If you need filter separately clicking link and keep current search then

       <% for tag in Model.order("name") %> #=> Model means which stored your tags like "Tag"
           <%= link_to tag.name, request.query_parameters.merge({tag: tag.name}) %>
       <% end %>
    

    See the below image for keep current search and filter by clicking on the link

    enter image description here

    These all are view functionality, you need to update your SQL query.

    I think it will help.