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 %>
Related
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
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.)
i am totally new to rails programming... i used the request.path to get the current url and display it in all my views by specifying it in applications.html.erb. It is returning the entire path and i want to display it as a link... so i use
link_to to specify it as url..now here is what i want to do.. the url returned will be in the format path1/path2/path3..... i want to display it as path1>path2>path3 and as a link such that when the user clicks path1, it should take him to path 1 and so on...
this is the code i gave in html.erb file
but i get an error that says undefined method.... what should i do to accomplish that??
You could split the request.path on / and then build up the various links, but that could get really cluttered for deeply nested paths. I think a better approach would be to use something like breadcrumbs_on_rails and declare your breadcrumbs explicitly and render them in a partial or helper method. I think you could also use some Rails filter magic to have action names breadcrumbed automatically, but making the breadcrumbs explicit forces you to think about your site and your users more than programmatically vomiting out a string of links of unknown length.
You can do like this:
<% path = request.path %>
<% links = path.split('/') %>
<% ll="/" %>
<% links.each do |l| %>
<% ll += (l+'/') %>
<%= link_to l,ll %> >
<% end %>
Im trying to take all the images in the 'app/assets/images/slide' folder and put them withing tags (in order). So, it will look like this :
<img src="1.jpg" >
<img src="2.jpg" >
<img src="3.jpg" >
How can I achive this? (Im using Rails 3.2.9)
Here's the code I tried (thanks to Khaled). But it outputs a plain text list of all image paths. I need the images to show :
#images = Dir.glob("app/assets/images/slide/*.jpg")
#images.each do |image|
image_tag image.gsub("app/assets/images/", "")
end
In your controller action, get all images paths.
#images = Dir.glob("app/assets/images/slide/*.jpg")
Then in your view (assuming haml)
- #images.each do |image|
= image_tag "slide/#{image.split('/').last}"
Assuming erb
<% #images.each do |image| %>
<%= image_tag "slide/#{image.split('/').last}" %>
<% end %>
To ensure the operation, you can use:
#images = Dir.glob("#{Rails.root}/app/assets/images/camisas/*.jpg")
Works for me and displays images:
Dir.glob('app/assets/images/slide/*').map do |path|
image_tag "slide/#{ File.basename(path) }"
end.reduce(&:+)
You want to get rid of the full path with File#basename so it works with precompiled assets afaik. You also don't need to specify .jpg because its an images folder, and could have .png's.
files = Dir.glob('app/assets/images/slide/*')
files.each do |file|
puts file
end
From the documentation, image_tag returns an html image tag for the source. It cannot show all images at once. You should make a custom helper to read and walk through your directory.
Greetings. I'm pretty sure there's a cleaner way to do this, but I can't find it.
I have a directory of icons users can pick to include in their content. I have a partial to create the palette of icons for them to choose from:
<% #files = Dir['public/images/prompts/*.*'] %>
<input type="hidden" id="test_prompt_image" value="/images/prompts/default.png" />
<% #files.each do |f| %>
<div onclick="$('#test_prompt_image').val('<%= f.gsub("public","") %>')" class="MultiColumn">
<img src="<%= f.gsub("public","") %>"/>
</div>
<% end %>
The results returned by the Dir include the full relative path on the server "public/images/...", but I have to remove "public" for the src path to find the image. Is there a call to use instead of Dir that returns a URI? I also messed around with including RAILS_ROOT in the directory path, but that just gave me a longer file path to clean into a request path.
Thanks!
By the way, to do things the Rails way (and good architecture), move the line:
#files = Dir['public/images/prompts/*.*'].map {|f| f.sub('public','') }
...out of the view and into the controller. The controller is the place to set up the variables and do data storage access, and the view is the place to display and format the info. And further, I would refactor this more by creating a constant for that path, e.g. in environment.rb:
ICON_DIRECTORY_PATH = 'public/images/prompts/'
Just do the removing of the public when you grab the list of files...
#files = Dir['public/images/prompts/*.*'].map {|f| f.sub('public','') }