Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:06:33+00:00 2026-06-15T06:06:33+00:00

I am using this example for file uploader. Now it works this way: I

  • 0

I am using this example for file uploader.

Now it works this way:
I upload a file, after the file is saved,the function(do_picture_analyse) calls R and produces a histogram(simplest version, in the more complicated version 2 packages have to be installed in R), picture of the histogram is saved. The problem is that if I want to upload 50 files, it takes lots of time to load 2 packages in R for each file separately(after_save callback).

What I need:
I upload a file, file is saved, I click on a button “Histogram” and the function do_picture analyses is called on all files that are in the database( It doesnt matter if some of the files have already been analyzed)

So I need only to know how to make an interaction between a button and a call of the function and nothing more.

My show.html.erb:

<script id="template-download" type="text/x-tmpl">
  {% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">

       <td></td>
      <td class="name">
       <a href="{%=file.url%}" download="{%=file.name%}">{%=file.name%}</a>
      </td>
       <td class="nam">
       <a href="{%=file.url_chip_image%}" download="{%=file.name%}">{%=file.name%}</a>
      </td>


      <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>     

      <td class="Pic">
        <button class="btn btn-mini btn-info">Pic</button>    
      </td>

      <td class="Hist">
        <button class="btn btn-mini btn-primary" >Hist</button>        
      </td>

      <td class="delete">
        <button class="btn btn-mini btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}"> 
          <i class="icon-trash icon-white"></i>

        </button>
        <input type="checkbox" name="delete" value="1">
      </td>
    </tr>
    {% } %}
</script>

my upload.rb:

def to_jq_upload
    {
      "name" => (read_attribute(:upload_file_name)).split(".").first,
      "size" => read_attribute(:upload_file_size),
      "url" => upload.url(:original),
      "delete_url" => upload_path(self),
      "delete_type" => "DELETE",
      "url_chip_image"=>read_attribute(:chip_image),

    }
  end


  after_save :do_picture_analyse


def do_picture_analyse
  if read_attribute(:chip_image)==nil

   require 'rinruby'
    myr = RinRuby.new(echo=false)

myr.filepath=upload.path(:original)
myr.fileurl=upload.url(:original)


myr.eval <<EOF
   s=read.table(filepath)
   for(j in nchar(filepath):1){
         if(substr(filepath,j,j)=="/"){      
           savepath<-substr(filepath,1,j-1)
           file.name<-filepath
           file.name<-substr(file.name,j+1,nchar(filepath)-4)

           break
         }
       }


  file.name1<-paste(file.name,"image.jpeg",sep="_")
  savepath<-paste(savepath,file.name1,sep="/")

   jpeg(filename=savepath,width=250, height=250)
   hist(s$V1) 
   dev.off()




EOF



  self.update_attributes(
    :chip_image => (((myr.fileurl).split("?").first)[6..-5]+'_image.jpeg')
  )
end

end

EDIT:

do_picture_analyse can take a folder as a parameter and analyse all files inside it by loading the the packages only one time for entire folder.There are only two folders for the files(two different types of files, let say .txt and .blabla files will be saved either in the txt-Folder or in a blabla-Folder. The type of the folder is saved in the database as well. By clicking the button, two folders should be passed to the do_picture_analyse and it will do everything
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:06:35+00:00Added an answer on June 15, 2026 at 6:06 am

    you need to create a new route for this :

    resources :name_of_your_controller do
      # use this if you want a route like resources/:id/analyze (single file)
      get :analyze, on: :member 
      # use this if you want a route like resources/analyze (multiple files)      
      get :analyze, on: :collection
    end
    

    then create a new action on your controller :

    def analyze
      # for single file analysis do something like this :
      @file = File.find( params[:id] )
      @file.do_picture_analyse
      respond_to do |format|
        # render what you need to render, js or html
      end
    
      # ... or do something like this for multiple file analysis :
      @files = File.where( params[:search] )
      @files.each {|f| f.do_picture_analyse )
      # etc.
    end
    

    you can then link your button to your action :

    # single file
    <%= link_to "Histogram", analyze_file_path( file ) %>
    # multiple files
    <%= link_to "Histogram", analyze_files_path( search: your_search_conditions ) %>
    

    PS: if your method needs a lot of processing power (if you use R, i assume that you have complex calculations involved), you should consider to extract it in a Worker to run it as a background task.

    edit

    response to your comments :

    i think you should extract this method and make it a class method, that accepts one or more paths.
    Then create a collection route that points to your controller ; in your controller action load the files according to some params and does something like this :

    # find the directories to be processed : 
    paths = @files.map(&:folder_type).uniq
    # pass them to your class method : 
    File.do_picture_analyse(paths)
    

    It is even possible to create a class methods that automatically handles these two steps for all files in a relation :

    def self.perform_analysis!
      paths = all.map(&:folder_type).uniq # or uniq.pluck(:folder_type) on rails >= 3.2.1
      do_picture_analyse(paths)
    end
    
    def self.do_picture_analyse( *paths )
      # call R from here
    end
    

    then you can do :

    File.where( your_search_params ).perform_analysis!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using plupload to make an ajax file uploading. Now the plupload.Uploader class has
I'm using the Handling file upload example. When uploading a file, the server shows
I used this example (http://reecon.wordpress.com/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/) to upload file from android device to server, it
I'm trying to style a TreeView using this example and everything was working fine
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
I have an array list of objects and I am using this example to
I'm using the apache POI to read Word documents. I'm using this example as
Just using this as an example... Here are the columns in my UserProfile table:
I have looked at this example using php and GD to piecewise-render a spiral
I'm using this simple node.js code example but I don't understand why FireBug doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.