I''m perty new to rails and I have an app where I want to have secure file downloads, so I can''t just drop the files in public/data, I need to use send_file. I found the post (pasted at bottom) that explained how to do this. When I go to implement it, I wanted to try the simplest thing first, so I have this code: datafiles_controller.rb: def download send_file(''/file_uploads/Productivity.pdf'', :disposition => :attachment) end datafiles/index.html.erb <%=button_to "Download File", :action=>''download''%> when I load index.html it gives the error "No route matches {:action=>"download", :controller=>"datafiles"}" so even though "resources :datafiles" is already in my routes.rb file, I added: get "datafiles/download" now the page loads, I click the "Download File" button and it gives me the error "No route matches "/datafiles/download"" I vaguely remember reading that every def in a controller should correspond to it''s own erb file, so I created Views/datafiles/ download.erb as a blank page. But that didn''t make any difference. I''m at a loss as to what I''m doing wrong, can someone give me some suggestions? post refered to above: -------------------------------------------------------- http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/9ad5649a7601045d/4d748526506abc59?lnk=gst&q=file+download#4d748526506abc59> Is there a way to retrieve a file off the server or hard drive? I > have a program that creates a file and I would like a button to > retrieve the file so the user can save it somewhere. Not ajax right > now.# view <%=button_to "Download File", :action=>''download''%> # controller def download send_file(path_to_file, :disposition => :attachment) end send_file documentation http://railsmanual.org/module/ActionController::Streaming/send_file/1... -- -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
resources :datafiles will create some default routes, but they don''t include download. You need to add a member route for the download action. http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ http://guides.rubyonrails.org/routing.html Luke On 2010-10-04, at 11:42 AM, Smashing wrote:> I''m perty new to rails and I have an app where I want to have secure > file downloads, so I can''t just drop the files in public/data, I need > to use send_file. I found the post (pasted at bottom) that explained > how to do this. When I go to implement it, I wanted to try the > simplest thing first, so I have this code: > > datafiles_controller.rb: > def download > send_file(''/file_uploads/Productivity.pdf'', :disposition > => :attachment) > end > datafiles/index.html.erb > <%=button_to "Download File", :action=>''download''%> > > when I load index.html it gives the error "No route matches > {:action=>"download", :controller=>"datafiles"}" > so even though "resources :datafiles" is already in my routes.rb file, > I added: get "datafiles/download" > now the page loads, I click the "Download File" button and it gives me > the error "No route matches "/datafiles/download"" > > I vaguely remember reading that every def in a controller should > correspond to it''s own erb file, so I created Views/datafiles/ > download.erb as a blank page. > But that didn''t make any difference. I''m at a loss as to what I''m > doing wrong, can someone give me some suggestions? > > > post refered to above: > -------------------------------------------------------- > http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/9ad5649a7601045d/4d748526506abc59?lnk=gst&q=file+download#4d748526506abc59 >> Is there a way to retrieve a file off the server or hard drive? I >> have a program that creates a file and I would like a button to >> retrieve the file so the user can save it somewhere. Not ajax right >> now. > > # view > <%=button_to "Download File", :action=>''download''%> > # controller > def download > send_file(path_to_file, :disposition => :attachment) > end > send_file documentation > http://railsmanual.org/module/ActionController::Streaming/send_file/1... > -- > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If I comment out everything in my index action in my datafiles_controller and paste in: send_file(''/file_uploads/Productivity.pdf'', :disposition => :attachment) it works, so obviously, the send_file command is good, the problem is with my routing. What do I need to add to my routes.rb file so I can create a new download action? or perhaps even more generally does someone know of a good guide that explains routing? I thought having "resources :datafiles" in my routes.rb file was enough and rails would just detect any new actions automatically. On Oct 4, 1:42 pm, Smashing <matt.lengenfel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m perty new to rails and I have an app where I want to have secure > file downloads, so I can''t just drop the files in public/data, I need > to use send_file. I found the post (pasted at bottom) that explained > how to do this. When I go to implement it, I wanted to try the > simplest thing first, so I have this code: > > datafiles_controller.rb: > def download > send_file(''/file_uploads/Productivity.pdf'', :disposition > => :attachment) > end > datafiles/index.html.erb > <%=button_to "Download File", :action=>''download''%> > > when I load index.html it gives the error "No route matches > {:action=>"download", :controller=>"datafiles"}" > so even though "resources :datafiles" is already in my routes.rb file, > I added: get "datafiles/download" > now the page loads, I click the "Download File" button and it gives me > the error "No route matches "/datafiles/download"" > > I vaguely remember reading that every def in a controller should > correspond to it''s own erb file, so I created Views/datafiles/ > download.erb as a blank page. > But that didn''t make any difference. I''m at a loss as to what I''m > doing wrong, can someone give me some suggestions? > > post refered to above: > --------------------------------------------------------http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > > Is there a way to retrieve a file off the server or hard drive? I > > have a program that creates a file and I would like a button to > > retrieve the file so the user can save it somewhere. Not ajax right > > now. > > # view > <%=button_to "Download File", :action=>''download''%> > # controller > def download > send_file(path_to_file, :disposition => :attachment) > end > send_file documentationhttp://railsmanual.org/module/ActionController::Streaming/send_file/1... > ---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
excellent, ty On Oct 5, 9:39 am, Luke Cowell <lcow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> resources :datafiles will create some default routes, but they don''t include download. You need to add a member route for the download action. > > http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/http://guides.rubyonrails.org/routing.html > > Luke > > On 2010-10-04, at 11:42 AM, Smashing wrote: > > > > > I''m perty new to rails and I have an app where I want to have secure > > file downloads, so I can''t just drop the files in public/data, I need > > to use send_file. I found the post (pasted at bottom) that explained > > how to do this. When I go to implement it, I wanted to try the > > simplest thing first, so I have this code: > > > datafiles_controller.rb: > > def download > > send_file(''/file_uploads/Productivity.pdf'', :disposition > > => :attachment) > > end > > datafiles/index.html.erb > > <%=button_to "Download File", :action=>''download''%> > > > when I load index.html it gives the error "No route matches > > {:action=>"download", :controller=>"datafiles"}" > > so even though "resources :datafiles" is already in my routes.rb file, > > I added: get "datafiles/download" > > now the page loads, I click the "Download File" button and it gives me > > the error "No route matches "/datafiles/download"" > > > I vaguely remember reading that every def in a controller should > > correspond to it''s own erb file, so I created Views/datafiles/ > > download.erb as a blank page. > > But that didn''t make any difference. I''m at a loss as to what I''m > > doing wrong, can someone give me some suggestions? > > > post refered to above: > > -------------------------------------------------------- > >http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > >> Is there a way to retrieve a file off the server or hard drive? I > >> have a program that creates a file and I would like a button to > >> retrieve the file so the user can save it somewhere. Not ajax right > >> now. > > > # view > > <%=button_to "Download File", :action=>''download''%> > > # controller > > def download > > send_file(path_to_file, :disposition => :attachment) > > end > > send_file documentation > >http://railsmanual.org/module/ActionController::Streaming/send_file/1... > > -- > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Are you going to make me do this for you ? ;) resources :datafiles do member do get :download end end And seriously, read this stuff: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ http://guides.rubyonrails.org/routing.html Luke On 2010-10-05, at 7:41 AM, Smashing wrote:> If I comment out everything in my index action in my > datafiles_controller > and paste in: > send_file(''/file_uploads/Productivity.pdf'', :disposition > => :attachment) > it works, so obviously, the send_file command is good, the > problem is with my routing. What do I need to add to my > routes.rb file so I can create a new download action? or perhaps > even more generally does someone know of a good guide that > explains routing? I thought having "resources :datafiles" in my > routes.rb file was enough and rails would just detect any new > actions automatically. > > > On Oct 4, 1:42 pm, Smashing <matt.lengenfel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I''m perty new to rails and I have an app where I want to have secure >> file downloads, so I can''t just drop the files in public/data, I need >> to use send_file. I found the post (pasted at bottom) that explained >> how to do this. When I go to implement it, I wanted to try the >> simplest thing first, so I have this code: >> >> datafiles_controller.rb: >> def download >> send_file(''/file_uploads/Productivity.pdf'', :disposition >> => :attachment) >> end >> datafiles/index.html.erb >> <%=button_to "Download File", :action=>''download''%> >> >> when I load index.html it gives the error "No route matches >> {:action=>"download", :controller=>"datafiles"}" >> so even though "resources :datafiles" is already in my routes.rb file, >> I added: get "datafiles/download" >> now the page loads, I click the "Download File" button and it gives me >> the error "No route matches "/datafiles/download"" >> >> I vaguely remember reading that every def in a controller should >> correspond to it''s own erb file, so I created Views/datafiles/ >> download.erb as a blank page. >> But that didn''t make any difference. I''m at a loss as to what I''m >> doing wrong, can someone give me some suggestions? >> >> post refered to above: >> --------------------------------------------------------http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... >> >>> Is there a way to retrieve a file off the server or hard drive? I >>> have a program that creates a file and I would like a button to >>> retrieve the file so the user can save it somewhere. Not ajax right >>> now. >> >> # view >> <%=button_to "Download File", :action=>''download''%> >> # controller >> def download >> send_file(path_to_file, :disposition => :attachment) >> end >> send_file documentationhttp://railsmanual.org/module/ActionController::Streaming/send_file/1... >> -- > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
in case someone else has similar problems, the code that I got working is: downloading: -------------------------------------------------------------- controller: datafiles_controller def download @datafile = Datafile.find(params[:id]) send_file(''/var/www/pub.website.com/file_uploads/''+ @datafile.name , :disposition => ''inline'') end View: index.html.erb <% for datafile in @datafiles %> <%= datafile.name %> <%= link_to("Download File", :action=>''download'',:controller=>''datafiles'',:id=>datafile) %> <% end %> routes.rb resources :datafiles do member do get :download end end uploading: --------------------------------------------------------------------------- datafiles_controller: def create u_id = session[:user_id] @file = params[:upload] post = Datafile.save(params[:upload],u_id) if post post = Datafile.event(u_id,"Uploaded","File", @file[''datafile''].original_filename) flash[:notice] = ''File has been uploaded successfully.'' redirect_to :action => ''new'' else flash[:notice] = ''File was not uploaded (invalid data).'' redirect_to :action => ''new'' end end datafiles view: new.html.erb <% form_tag( { :action => ''create'' }, :multipart => true ) do %> <% flash.each do |key, value| %> <%=value%> <% end %> <div class="label_div">Select File:</div> <%= file_field ''upload'', ''datafile'' %> <%= submit_tag "Upload" %> <% end %> datafiles view: index.html.erb <% for datafile in @datafiles %> <tr> <td class="tt"> <%= datafile.name %> <%= link_to("Download File", :action=>''download'',:controller=>''datafiles'',:id=>datafile) %> </td> <% @user = User.find(datafile.user_id) %> <td class="tt"><%= @user.full_name %></td> <td class="tt"><%= datafile.created_at.strftime("%m/%d/%Y at %I: %M%p") %></td> </tr> <% end %> On Oct 4, 1:42 pm, Smashing <matt.lengenfel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m perty new to rails and I have an app where I want to have secure > file downloads, so I can''t just drop the files in public/data, I need > to use send_file. I found the post (pasted at bottom) that explained > how to do this. When I go to implement it, I wanted to try the > simplest thing first, so I have this code: > > datafiles_controller.rb: > def download > send_file(''/file_uploads/Productivity.pdf'', :disposition > => :attachment) > end > datafiles/index.html.erb > <%=button_to "Download File", :action=>''download''%> > > when I load index.html it gives the error "No route matches > {:action=>"download", :controller=>"datafiles"}" > so even though "resources :datafiles" is already in my routes.rb file, > I added: get "datafiles/download" > now the page loads, I click the "Download File" button and it gives me > the error "No route matches "/datafiles/download"" > > I vaguely remember reading that every def in a controller should > correspond to it''s own erb file, so I created Views/datafiles/ > download.erb as a blank page. > But that didn''t make any difference. I''m at a loss as to what I''m > doing wrong, can someone give me some suggestions? > > post refered to above: > --------------------------------------------------------http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > > Is there a way to retrieve a file off the server or hard drive? I > > have a program that creates a file and I would like a button to > > retrieve the file so the user can save it somewhere. Not ajax right > > now. > > # view > <%=button_to "Download File", :action=>''download''%> > # controller > def download > send_file(path_to_file, :disposition => :attachment) > end > send_file documentationhttp://railsmanual.org/module/ActionController::Streaming/send_file/1... > ---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.