Rails: Path of File

Rails: path of file

file = File.join(Rails.root, 'app', 'csv', 'names.csv')
File.read(file)

Pass Uploaded File path To File.read(path)

When you upload a file with a form like the one in your question then the following should work:

File.read(params[:file].tempfile.path)

Or even simpler:

params[:file].read

Rails: path in File.read

Try this

def welcome_email(message)
@message = message
@image=@message.pics_file_name #Getting file name

#you need to give application root path here
attachments[@image]=File.read("#{Rails.root}/your assets path/#{@image}") #Have to get path here
mail(:to => @message.mailto, :subject => 'Welcome to my site')
end

your assets path may be , if you are storing the assets in public directory then path will be

attachments[@image]=File.read("#{Rails.root}/public/assets/#{@image}")

Rails file location and access

Without configuring routes and controllers, client can only access files in the public/ directory (it doesn't matter what the extension is).

Bare in mind this: when your Rails app is run by webserver, its webroot will be the public directory, consequently to access public/file.ext you request should be webroot/file.ext

Rails: Use of absolute path in Rails 6

In your case it looks like you want to render a normal view, i.e. a template.

In that case using the file option is not the recommended way. Instead you should be using the template option.

render template: 'devise/sessions/new'

Or even better, you can use this shortcut:

render 'devise/sessions/new'

Background

The file option is intended to render a view which is outside your Rails application, where you can't rely on Rails' view lookup logic. Consequently Rails wants to have an absolute path. Demanding an absolute path also forces the developer to think about relative path segments (/../).

Omitting the .slim extension and then having the file processed by the template engine is a feature intended for templates. Using file seems to provide the very same functionality, but my guess is that this is just a side effect of the internal workings of the view path lookup. It looks like the Rails developers want to improve the distrinction between files and templates in the future and deprecating relative files is an intermediate step to not break too many existing applications which rely on using file and still expect the features of a template.

PS: It is not necessary to manually split your path. So if you for some reason still want to use file with an absolute path, instead of

render file: Rails.root.join('app', 'views', 'devise', 'sessions', 'new.html.slim')

use this

render file: Rails.root.join('app/views/devise/sessions/new.html.slim')

Ruby on Rails - file path is different on development and production?

The issue was /

This works on development & production.

sftp.download!("1.txt", "public/file.txt")


Related Topics



Leave a reply



Submit