问题描述:

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

网友答案:

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.

网友答案:

For your case, which I believe is you have to select either set of submit button(files/options). You should be doing something like this:

Controller:

  def update  
    @counter = Counter.find(params[:id])
      # here we need to check which button is pressed & based on that call that particular method, either update file/option.
    if params[:commit] == 'update_options'
      update_options
    elsif params[:commit] == 'update_files'
      update_files
    end    
  end



def update_options
  # method you want to write
end


def update_files
  # method you want to invoke
end

View:

  <div class="field">
    <%= f.label :Options %><br />
    <%= f.text_field :options %>
    <%= f.submit 'update_options' %>
  </div>
  <div class="field">
    <%= f.label :Files %><br />
    <%= f.text_field :files %>
    <%= f.submit 'update_files' %>
  </div>

Hope it helps! :)

网友答案:

If you open up Firebug/Chrome inspector and see what happens after any selection is made in the checkbox demo you can see that they make a GET request and pass the value of the checkbox selected to the server. Thats exactly what you can implement.

Pass the value of the selected checkboxes to the server and then based on that process things on your server. The params passed to the controller should show you the values being passed.

Small suggestion as you go forward, you can move the code that gives you details of the file into a model. That way although you are not associated with the db(do not inherit ActiveRecord in this model class) but still you adhere to the MVC principles.

网友答案:

Here's what I understand your question is like:

You have

  • File 1

    • Pictures <-- check
    • Info <-- check
    • Normalization
    • Scatterplots
  • File 2

    • Pictures
    • Info <-- check
    • Normalization <-- check
    • Scatterplots

First create and array like

@file_options = [["Pictures", "pics"], ["Info", "info"],["Normalization", "norm"],["Scatterplots", "scat"] ]

And in your view you could do something like:

<div class="my_profile_info">     
  <p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>
  <td> <%=select_tag "file_options[#{file.id}][]", options_for_select(@file_options), :multiple => true %></td>        
</div>

In you controller:

params[:files].each do |file_id|
  params[:file_options][:file_id].each do |option|
    # do something according to "pics" or "info" etc..
  end
end

Note: To select multiple options you may have to use 'ctrl' in some browsers/OS.

网友答案:

I've had to do something like this before. Basically what we did was we used javascript to change the value of a hidden tag.

Basically you set up ids for the submit buttons and you put in an onClick event. We typically use JQuery, I assume you are too since rails comes with it packaged it most of the time. But basically within your document.ready() when they on click the delete button, I set the value of my hidden tag to "delete" and when they click the analyze button I set the value of the hidden tag to "analyze". Then in your controller when it comes in just check the value of hte parameter for the name of the hidden tag. Easier explained than coded... but it turns out I was editing that file right now so you can take a look:

    <script>
      jQuery(document).ready(function()
      {
        $('#unassign_button').click(function()
        {
          $('#action').val("unassign");
          $('#action_form').submit();
        });
        $('#add_button').click(function()
        {
          $('#action').val("add");
          $('#action_form').submit();
        });
      });
    </script>

That is the javascript snippet I am using. I have a hidden tag with id action and a two buttons one with id unassign_button and the other with add_button. The action_form is the id of my form so that I can submit it.

The issue with having two submit buttons is they do not hold a value to send a parameter and they both submit the same form to the same URL so you need some sort of input tag to carry the value of which button was clicked.

I typically just write the button in HTML rather than with ruby tags since I typically will just submit the form from the javascript at that point as well -- the buttons are just decorative at that point and its easier for the HTMl guy to style them without the ruby so my buttons and hidden tag look like:

<%= hidden_tag :action, "", :id=>"action" %>
<button class="add_button" id="add_button">Add</button>
<button class="unassign_button" id="unassign_button">Unassign</button>

and in the controller I just look at params[:action] to see which button they clicked.

EDITED:

I just found this:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"3lIUwiKQG8hoYXIkPS9RU4GANGR0yGpJkAAyg2WTD0U=", "short_code"=>"tsCe0yFiWL", "commit"=>"Launch", "remote"=>"true"}

which means you can look at the commit param to see which button they clicked...

相关阅读:
Top