1

I've got two Rails apps. How would I delete a file in /public on the remote app from the local app? Pretty vague question I realise, just not sure how to start it all?

Thanks!

t56k
  • 6,769
  • 9
  • 52
  • 115
  • 1
    Why not just log in and delete the file? Are you wanting to do this often? Are you using a deploy system like capistrano? – noel Mar 05 '14 at 00:08
  • The remote app stores files available for download. If the master file on the local app should be deleted I'd want its related files on the remote app deleted too. Not using Capistrano. – t56k Mar 05 '14 at 00:12
  • How are you hosting the app? Self-hosted or through Heroku or what? – Tyler Mar 05 '14 at 00:31
  • You should really store these files outside of the app space, IMO. – Dave Newton Mar 05 '14 at 01:26
  • Self-hosted files, yeah. I can write a method in the remote Rails app if that's best way to go about this? – t56k Mar 05 '14 at 02:03
  • @DaveNewton Why's that? – t56k Mar 05 '14 at 02:03
  • 1
    Because keeping non-app artifacts in the app file space is just trouble. It prevents trivial redeployment, it means you can't just wipe out the app and start over, etc. The app file space is a place for developers, not for users, to control. – Dave Newton Mar 05 '14 at 03:29
  • @emm You might consider capistrano. For one, you can make a simple deploy task that synchronizes the public directory, deleting any files missing in the source ([something like this](http://stackoverflow.com/questions/1661586/how-can-you-check-to-see-if-a-file-exists-on-the-remote-server-in-capistrano)). But also, you can easily redeploy and reset public to your repository. – noel Mar 05 '14 at 15:23

1 Answers1

3

Try this

In your controller action

if File.exist?(file_path)
   File.delete(file_path)
end

Update

First you need to allow CORS Requests in your Rails app

In your application_controller.rb add the following lines

#application_controller.rb    
class ApplicationController < ActionController::Base

  before_filter :allow_cors_requests

  def allow_cors_requests
    headers["Access-Control-Allow-Origin"] = "*"
    headers["Access-Control-Allow-Methods"] = "GET, PUT, POST, DELETE" # In your case just use delete
    headers["Access-Control-Allow-Headers"] = "Content-Type, X-Requested-With, X-CSRF-Token, Origin Accept"
    head(:ok) if request.request_method == "OPTIONS"
  end

end

Now from your controller action make a ajax request to delete a file.

Monideep
  • 2,790
  • 18
  • 19
  • Haven't heard of CORS requests before? Thanks for the suggestion. Will investigate. – t56k Mar 05 '14 at 02:46
  • @deep: Doesn't that open the app up to cross site script attacks? – noel Mar 05 '14 at 15:24
  • @noel yup!! You can use Access-Control-Allow-Origin to specify trusted origins. CORS requests are allowed only if the origin header is correct. But that doesn't mean your app is secure never trust a client request you still need to validate each and every request the client sends. The best way to implement cross domain ajax requests is to implement some authentication mechanism where you validate each requests against a secret token. – Monideep Mar 20 '14 at 11:37