Ruby On Rails file selector - ruby-on-rails

I am developing my first Ruby On Rails project and I have the following question.
Is it possible to display a window with the contents of a folder so I can select a file? For example, images from the "app/assets/images" project folder.
I have searched in Google and rubygems.org but have not found anything.
Respectfully,
Jorge Maldonado

You could write a controller method and associated view that scan over a folder and output the contents. Here's an example.
Controller:
def folder
#files = Dir.open(Rails.root.join('public', 'images'))
end
View:
<table>
<% #files.each do |file| %>
<tr><td><%= link_to file, file_path(file) %></td></tr>
<% end %>
</table>
Note that file_path is assumed to be some route defined in routes.rb which accepts a file name and allows the user to view/open/download that file.
Here's a gem that also gives similar functionality:
https://github.com/furkancelik/fcfinder

Related

Ruby on Rails Paper clip old files name issue

I'm using Rails 4 and after added paper clip initializer code to initialize the user files name by user ID to give every file different name using this code
In the initialize file
# initializers images names by the model id (:id)
Paperclip.interpolates :parent_id do |a, s|
a.instance.contract.id
end
In the model
has_attached_file :photo ,
:url => "/system/files/users/:basename_:id.:extension",
:path => ":rails_root/public/system/files/users/:basename_:id.:extension"
Now it's work well by give the file his basename_fileID.file extension
but I notes that I was missed all the old files links because the new link will search about the file contain the basename and fileID something like that "image_1.jpeg" but the old link search about basename only something like "image.jpeg"
so how I can solve it in Rails without change my old files name on server ??
Is there's some thing in Rails 4 allow me to inform the links to get the old path in some cases or not ??
Finally I solved this issue by check if the file exist on server or not by using File.exist?
The code
<% if File.exist?(#user.photo.path) %>
<!--The new path -->
<%= link_to "Photo", #user.photo.url %>
<% else %>
<!--The old path -->
<%= link_to root_url+'system/files/users/'+#user.photo_file_name, :title => "show image" do %>Photo<% end %>
<% end %>

Ruby on rails Print Directory

I am pretty new to ruby and RoR, so please pardon my noviceness.
I want to print complete list of the folders inside a given directory in my rails application. Following is the code I use in my show.html.erb file.
<%= dir = Dir.entries(path_to_my_directory)
dir.each do |folder|
puts folder
end %>
The Dir.entries method return an array, containing the names of all the folders inside the specified directory. I traverse this array and print every single value.
But on my application, this prints the complete array like mentioned below.
[".", "metadata.rb", "attributes", "libraries", "CHANGELOG.md", "recipes", "..", "files", "templates", "providers", "resources", "definitions", "README.md"]
I tried other ways to traverse an array but the output remains the same. This code when run individually produces the expected output, but when ran from inside my view, it doesn't seem to work. Any suggestions on where I might be going wrong.
Thanks in advance.
You can't use puts in erb. Find the difference between <% %>(processing the Ruby code) and <%= %>(processing and outputs the enclosed Ruby code). What you wanted to do is:
<%
dir = Dir.entries(path_to_my_directory)
dir.each do |folder|
%>
<%= folder %>
<% end %>

ruby on rails directory browser

I'm trying to write a simple RoR app that lists all directories in a specific path then allows me to click that file or directory to either open the file or open the directory. I'm using the following code which works to list the directories but I can't figure out how to open the file or directory from here. When clicking the directory I get a "No route matches [GET] "/selected file" error.
controller
#file = Dir.foreach ("/specifiedpath/")
view
<% #file.each do |file| %>
<tr>
<td><%= link_to file, file %></td>
</tr>
<% end %>
Any help would be appreciated!
You need something in your routes.rb that looks like this:
get '/browse/*path' => 'browse#show', as: :browse
In your browse_controller, you'll want a show action that does something like:
class BrowseController < ApplicationController
def show
#path = params[:path] || '/some/default'
#files = Dir.foreach(path)
end
Finally, in your browse/show.html.haml (I prefer HAML, sorry) view, something like:
- #files.each do |file|
%tr
%td= link_to file, browse_path(File.join(#path, file))
(Pardon me if it all doesn't work outright—haven't time to fully test a fleshed out solution right now—but I can delve into it a little more if you have trouble getting it to work yourself.)

How to add image/template/js/css load paths based on variable within app controller?

So, I have a bunch of partners whose templates and images are stored below the view, public/images, public/stylesheets, public/javascripts directories.
For example, images for a partner 'foo' are stored in public/images/foo
This partner is an instance variable #partner which is accessible at the application level.
Problem is, I'm doing this all over the place: <%= image_tag "/images/#{#partner}/image.jpg" %> or within locations_controller: <% redirect_to "/locations/#{#partner}/index" %> ...
One reason for the load path part of the question rather than using helpers: we have to specifically <% render :template => "/locations/#{#partner}/index" %> since Rails looks in /locations/index by default.
How could I make this easier on myself? How could I add to load path once I have #partner?
Using Ruby 1.8.7 and Rails 2.3.4
Your redirection in the controller is really bad practice
you should do something like:
redirect_to get_path(#partner)
then in your controller
def get_path(partner)
case partner
when "partner1"
partner1_path
...
end
end
Concerning your pictures, you should create a helper.
def get_pic(partner, image)
image_tag "/images/#{partner}/#{image}"
end
And in your view
<%= get_pic(#partner, "image.jpg" %>

Web page that links all files in a directory

I want to build a ruby web page that dynamically links all files in a directory. Does anyone have any sample code or basic suggestions as to how to do it?
Use the Dir class, either with Dir.entries to list the directory, or with Dir.glob for a bit more flexibility. Keep in mind that entries gives you names only, while glob will include the full relative path.
You could use an action like this:
def index
root = "#{RAILS_ROOT}/public"
#files = Dir.entries(root).reject {|x| x.match /^\./}
end
And a view:
<% #files.each do |path| %>
<li><a href="<%= path %>">
<%= File.basename path %></a>
<% end %>

Resources