Search code examples
ruby-on-railscheckboxsubmit-button

Ruby on Rails, submit_tag with 3 checkboxes


I need something like this but in Ruby On Rails. I have a code that selectes specific files to be either deleted or analyzed:

<% if @files%>    
<%= form_tag what_to_do_files_path, method: :get do %>
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
  <% @files.each do |file| %>
    <% if (arraydb.file=="no") %>
        <p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>       

    <% else %>      

    <div class="my_profile_info">     
    <p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>        

    <td class="Info">
    <a href="<%=file.info%>" target ="_blank" class= "btn btn-mini btn-info">Info</a>  
    </td>  

     </div>

    <% end %>
  <%end%>
<%end%> 
<%else%>
<%end%>

routes:

resources :files do
      collection do             
        get :what_to_do      
      end
    end

controller:

def what_to_do
  method=params[:commit]
    if method == 'Delete selected'
     do smt

   elsif  method == 'Analyze'
     do another thing
   end
end

What I need to have are options for the "Analyze" button. Like Pictures, Info, Normalization, Scatterplots with a checkbox. So, I select Pictures and Normalization and klick on "Analyse", so only rthose two options will be proceeded. I am not quite sure how I can wrap a checkbox to a submit_tag.

EDIT

Example
choose option (checkbox)

  • Normalization
  • Pic
  • Scatterplots
  • Info

choose files(checkbox):

  • File1
  • File2
  • File3

Solution

  • I'm not a 100% sure if I understand what you are after, but this is my understanding:

    You have a bunch of files to pick from with checkboxes, you also need option checkboxes to pass additional options to the Analyze action?

    Just add inside your form_tag:

    <%= check_box_tag "pictures", true %>
    <%= check_box_tag "normalization", true %>
    

    and so on.

    In your controller:

    elsif method == "Analyze"
     if params[:pictures]
      do picture stuff
     if params[:normalization]
      do normalization stuff
    

    etc etc etc params[:nameofcheckboxtag] will either be set to the value (second option, true in my example) if the checkbox is checked, otherwise it won't exist. Either way, only the checked options will be processed with this example.