ruby on rails directory browser - ruby-on-rails

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.)

Related

Ruby On Rails file selector

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

How to give the xls file link in Ruby on Rails?

How to give the xls file link in ruby. This is my file path
link_to "Excel", "/#{RAILS_ROOT}/public/reports/10014_ByNetwork.xls", :target=>"_blank"
when i given above link that is converting like this
Excel
So its not working. actually i need like this
Excel
Please give me exact path...
#Gagan Your syntax is incorrect. You should test your answers before posting them. This is the correct way:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", target: "_blank" %>
OR
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", :target=>"_blank" %>
You have missed out the comma after the second closing double quotes.
if you have more than one file to download and you have to use different links along your views I could recommend you the next approach too:
Add a download xls get resource and route helper to your routes.rb file like:
get "downloads/xls/:id" => "downloads#xls", :as => :download_xls
In your controller, for my my example I'll use app/controllers/downloads_controller.rb we will need to add the xls action to stream data with send_file:
def xls
if params[:id]
send_file("#{Rails.root}/public/reports/#{params[:id]}.xls",
filename: "#{params[:id]}.xls",
type: 'application/excel',
disposition: 'attachment')
end
end
You can read more about it here:
http://apidock.com/rails/ActionController/DataStreaming/send_file
And finally in your view you'll use the link_to helper with our declared above download_xls_path route and the filename as param:
<p>
Click to download: </br>
<%= link_to "NameOfYourXlsFile.xls", download_xls_path(NameOfYourXlsFile) %>
</p>
Are you sure that you need file:// link? It is not operated by rails server and will work only on your machine. Maybe it will be better to generate link like this: "http://you_server_url.org/public/file.xml". It works fine on local and remote app, and you can manage file-sending by controller action.
Try this in your view file:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls" :target=>"_blank" %>

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 %>

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 %>

Missing template

so i have this code:
<% form_tag(:action => 'find') do%>
Product name:
<%= text_field("cars_", "name", :size => "30") %>
<input type ="submit" value="Find"/>
<%end%>
upon pressing the button I want it to complete the method (def find) found in the controller but its requesting the html.erb file:
Template is missing
Missing template cars/find.erb in view
path H:\Documents and
Settings/owner/My
Documents/NetBeansProjects/RailsApplication5/app/views
in the find def (found in controller)
def find
#car = Car.new(params[:car_])
end
What is the last line of your find method? Generally, if you don't specify the template to render in your controller method, Rails attempts to find a template with the same name as the method. That is why it is saying it can't find cars/find.erb. Without seeing the code in your find action, it is hard to give a better answer.
Your find method should be doing some searching, not initializing with the parameters. I recommend checking out something like thinking sphinx or searchlogic if you want to do searching.
I believe that your code is executing the find action. However, after it finds the car object, it needs to write that into a template that shows the results of your search. By convention, rails looks for a file called find.html.erb in the view folder for that controller. So, the error message you are seeing means that Rails has executed the line of code in your action and is now trying to generate some HTML to send back to the browser
If you create a simple file in the view folder for that controller with contents:
<%= #car.name %>
You should see the results.
However, your code is a bit confusing to me as I don't know why a find method would create a new Car object. I would expect something like this:
def find
#car = Car.find_by_name(params[:name])
end
I would also expect that your form would be more like:
<% form_tag(:action => 'find') do%>
Product name:
<%= text_field_tag("name", :size => "30") %>
<%= submit_tag "find" %>
<%end%>

Resources