I uploaded pdf documents on s3 using carrierwave and fog. Is there a way for users to preview content without downloading it?
By the end of the day i realised that i might have not only pdf but other types of files. So in fog.rb i set public to false so it generates uniqe url's
config.fog_public = false
And in my view.html.erb i do a simple link_to so it would open document in a new window without saving it like this:
<%= link_to "View CV", #applicant.cover.attachment.url, :target => "_blank" %>
Maybe there is way to preview links but for the moment that's fine.
You might consider using PDF-JS. There is a Rails gem but it hasn't been updated in 2 years. I would probably just download the latest version.
Is there a way to open .html.erb files by themselves in the browser. The Rails app I am trying to have a look at is in rails 2.3 and will not run locally on the rails server nor will it run when I give the command script/server.
I would just like to open the individual views in the browser by themselves...is this possible?
You can open them in the browser, but the <% %> and <%= %> tags will get shown as-is instead of executing the Ruby code. These are template files that need to get processed by Rails to fill in data at runtime. Your best bet is TsaiKoga's answer, making a controller action render the template in question.
In rails4 you can do it like this:
erb a.html.erb > a.html
Maybe it doesn't work in rails 2.3. If you want to render that view, maybe you can use an action just like users#index and change the code to render it:
render "devise/mailer/a.html.erb", :layout => false
Please help... :) Working on this for a couple of days now
I have a submittal database with stored url's to manufacturer installation instructions generally in PDF formats. I don't want to use the default to_path because I'll have to store the documents locally on my server. Here's the snippet of code that I'm using to pull the url path from my database. As you can see below without the path_to rails wants to turn the url into a path (see error below). I tried adding quotes but then rails doesn't read the code correctly. I've read about helpers but haven't gotten them to work.
<%= #wf_room.wf_lights.pluck(:typemark).count %> <%= link_to #wf_room.wf_lights.map(&:typemark).uniq, #wf_room.wf_lights.map(&:url).uniq %>
error:
undefined method `http://www.sistemalux.com/en/files/ficheproduit/7050_Sliver_wall(13).pdf_path' for #<#:0x0000000404bad8>
Have you tried string interpolation?
<%= link_to #wf_room.wf_lights.map(&:typemark).uniq.first, "#{#wf_room.wf_lights.map(&:url).uniq.first}" %>
(I added first method because won't they the current methods return arrays?)
I'm new to rails, and I'm writing a RESTful website using the CRUD technique. So far I have created three pages, all of which allow the user to create, edit, and delete a row from the database. However, my fourth page will need to include an upload file form, but a) I don't know how the filesystem works with Rails thus I don't know where files should be stored. The file would be around 100kb and couldn't be stored in temporary storage because it will be constantly downloaded. And b) I don't know how to write to a file.
It would be great if you could tell me how to do what I mentioned above - create an upload input on an input form, and to then write the file to a filepath in a separate directory.
Update 2018
While everything written below still holds true, Rails 5.2 now includes active_storage, which allows stuff like uploading directly to S3 (or other cloud storage services), image transformations, etc. You should check out the rails guide and decide for yourself what fits your needs.
While there are plenty of gems that solve file uploading pretty nicely (see https://www.ruby-toolbox.com/categories/rails_file_uploads for a list), rails has built-in helpers which make it easy to roll your own solution.
Use the file_field-form helper in your form, and rails handles the uploading for you:
<%= form_for #person do |f| %>
<%= f.file_field :picture %>
<% end %>
You will have access in the controller to the uploaded file as follows:
uploaded_io = params[:person][:picture]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
It depends on the complexity of what you want to achieve, but this is totally sufficient for easy file uploading/downloading tasks. This example is taken from the rails guides, you can go there for further information: http://guides.rubyonrails.org/form_helpers.html#uploading-files
Sept 2018
For anyone checking this question recently, Rails 5.2+ now has ActiveStorage by default & I highly recommend checking it out.
Since it is part of the core Rails 5.2+ now, it is very well integrated & has excellent capabilities out of the box (still all other well-known gems like Carrierwave, Shrine, paperclip,... are great but this one offers very good features that we can consider for any new Rails project)
Paperclip team deprecated the gem in favor of the Rails ActiveStorage.
Here is the github page for the ActiveStorage & plenty of resources are available everywhere
Also I found this video to be very helpful to understand the features of Activestorage
There is a nice gem especially for uploading files : carrierwave. If the wiki does not help , there is a nice RailsCast about the best way to use it . Summarizing , there is a field type file in Rails forms , which invokes the file upload dialog. You can use it , but the 'magic' is done by carrierwave gem .
I don't know what do you mean with "how to write to a file" , but I hope this is a nice start.
Okay. If you do not want to store the file in database and store in the application, like assets (custom folder), you can define non-db instance variable defined by attr_accessor: document and use form_for - f.file_field to get the file,
In controller,
#person = Person.new(person_params)
Here person_params return whitelisted params[:person] (define yourself)
Save file as,
dir = "#{Rails.root}/app/assets/custom_path"
FileUtils.mkdir(dir) unless File.directory? dir
document = #person.document.document_file_name # check document uploaded params
File.copy_stream(#font.document, "#{dir}/#{document}")
Note, Add this path in .gitignore & if you want to use this file again add this path asset_pathan of application by application.rb
Whenever form read file field, it get store in tmp folder, later you can store at your place, I gave example to store at assets
note: Storing files like this will increase the size of the application, better to store in the database using paperclip.
In your intiallizer/carrierwave.rb
if Rails.env.development? || Rails.env.test?
config.storage = :file
config.root = "#{Rails.root}/public"
if Rails.env.test?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
end
end
end
use this to store in a file while running on local
How would this be updated for Rails 3.1?
http://railscasts.com/episodes/88-dynamic-select-menus
I just can't figure out how to call the js.erb file and have it run the code to generate the javascript dynamically.
Might be something: in Rails 3.1, you're most likely using jQuery instead of Prototype. The example code on the Railscasts site is using good old Prototype instead of the new hotness that is jQuery (default javascript library in Rails 3.1).
Once all your jquery pipes are connected, having rails respond to and render your js.erb is the same as always. In your controller:
def country_selected
// whatever you need to do
respond_to do |format|
format.js
end
end
Then in your view directory, you have a country_selected.js.erb that you can put in whatever javascript you want to update the second select menu. (Remember you have to escape your shiz for it to work correctly) e.g.
<%= escape_javascript(params[:country]) %>
By the way, I think .rjs was moved out of Rails proper and into it's own Gem. Something else to keep in mind regarding Rails 3.1 vs. javascript.