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
What Is the Point of Object#Presence in Rails
How to Resolve Deprecation Warnings for Openssl::Cipher::Cipher#Encrypt
Why Is "❨╯°□°❩╯︵┻━┻" with Such an Encoding Used for a Method Name
Import SASS Partial Over Http Instead of Filesystem
How to Elegantly Symbolize_Keys for a 'Nested' Hash
How to Use the Ruby "Self" Keyword
Add_Foreign_Key VS Add_Reference in Rails
Ruby on Rails Named Scope Implementation
Trying to Understand Use of Self.Method_Name VS. Classname.Method_Name in Ruby
Ruby W/ Sinatra: What Is the Equivalent of a .Js.Erb from Rails
Conditional Haml - If Else Nesting
How to Modify the Input Type of the Rails Datetime_Select Helper
Rails Change Routing of Submit in Form_For
How to Make a Ruby Script Run Once a Second