0

I am using file uploading in my ruby on rails application. And when the file is uploaded, I want to make the link for others so that they can also download that file. I have a folder named job_attachments within the assets folder for the file attachments. I want to know how I can get the the path for job_attachments using jQuery.

I am doing something like this in my controller:

  def uploadattachment
    if(params[:job][:uploaded_data])
        uploaded_io=params[:job][:uploaded_data]
        File.open(Rails.root.join('app/assets', 'job_attachments', uploaded_io.original_filename), 'wb') do |f| 
              f.write(uploaded_io.read) 
        end

    respond_to do |format|
      format.json {render :layout=>false , :json => uploaded_io.original_filename}      
    end
end

end

1 Answer 1

2

You probably don't want to put uploaded content into your assets folder. Assets are for things that you create that are part of your application. Additionally, app/assets isn't directly accessible by clients. I assume you want people to be able to download these files?

Create another folder under public ("uploads", perhaps), and put your files there.

To get the path in jQuery, just return the path from your controller when the file is uploaded. You probably want to turn a hash, rather than a string, since that's a little easier to deal with in jQuery.

def uploadattachment
  if(params[:job][:uploaded_data])
      uploaded_io=params[:job][:uploaded_data]
      File.open(Rails.root.join('public', 'uploads', 'job_attachments', uploaded_io.original_filename), 'wb') do |f| 
            f.write(uploaded_io.read) 
      end

      respond_to do |format|
        format.json {render :layout=>false , :json => {:path => File.join('uploads', uploaded_io.original_filename}}      
      end
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

The files are uploading perfectly in jobs_attachment folder. Why I need to upload them into public directory ?
Updated answer. app/assets will work in development, but in production mode you can't access those files directly. They get precompiled into public/assets. This won't happen for anything you've added after deployment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.