mysql - ruby on rails file download binary files from the database and provide a link -


i have 3 files in mysql database. 2 of them of type 'text' , 1 'longblob'.

|username| |pem      | |key       | |cert              |  |test1   | |some text| |some text | | binary text |  |test2   | |some text| |some text | | binary text | 

i can query them , have array activerecord relation object in @user_files variable.

now, want create 3 files filename1.pem, filename1.key, filename1.cert each user , provide them link download.

something like:

<%- @user_files.each |user_info| %>   <tr>     <td><%= link_to 'download files', download_path(user_info) %></td>   </tr> <% end %> 

here had in mind far in controller:

 def download(user_file)     temp = tempfile.new("temp-filename-#{time.now}")     zip::zipoutputstream.open(temp.path) |z|       z.put_next_entry("some_name.pem")       z.print io.read(user_file.pem)       z.put_next_entry("some_name.key")       z.print io.read(user_file.key)       z.put_next_entry("some_name.cert")       z.print io.read(user_file.cert)      end      send_file temp.path, :type => 'application/octet-stream', :filename => "some_name.zip"     temp.close    end 

i getting following error:

wrong number of arguments (0 1) app/controllers/my_controller.rb:16:in `download' 

my routes follows:

get 'download' => 'my_controller#download', as: :download 

my first question if right approach , work? appreciate if guide me cleaner approach requirement.

edit: updated use params

<%= link_to 'download files, download_path(user_info) %>

def download   user_info = params[:id]   #remaining code end 

also error afte rthe route update suggested in answer:

no route matches [get] "/download/415" 

controller action must not have arguments. pass controller action accessed params variable hash (hashwithindifferentaccess actually). that, download action might like:

 def download     user_file = params[:id]     temp = tempfile.new("temp-filename-#{time.now}")     zip::zipoutputstream.open(temp.path) |z|       z.put_next_entry("some_name.pem")       z.print io.read(user_file.pem)       z.put_next_entry("some_name.key")       z.print io.read(user_file.key)       z.put_next_entry("some_name.cert")       z.print io.read(user_file.cert)      end      send_file temp.path, :type => 'application/octet-stream', :filename => "some_name.zip"     temp.close    end 

also, change route into:

get 'download/:id' => 'my_controller#download', as: :download 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -