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','') }
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 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 %>
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.
I built a .erb file that has a bunch of variables listed in it.
<body>
<h1>
<%= header %>
</h1>
<p>
<%= intro1 %>
</p>
<p>
<%= content1 %>
</p>
<p>
<%= content2 %>
</p>
<p>
<%= content3 %>
</p>
</body>
Then I have a text file with the variables:
header=This is the header
intro1=This is the text for intro 1
content1=This is the content for content 1
content2=This is the content for content 2
content3=This is the content for content 3
I need to take the variables from the text file and insert them into the .erb template. What is the proper way to do this? I am thinking just a ruby script, rather than an entire rails site. It is only for a small page, but needs to be done multiple times.
Thanks
I think a lot of people have come at this from the "how do I get the values out of where they are stored?" and have ignored the other half of the question: "How do I replace <%= intro1 %> with some Ruby variable I have in memory?
Something like this should work:
require 'erb'
original_contents = File.read(path_to_erb_file)
template = ERB.new(original_contents)
intro1 = "Hello World"
rendered_text = template.result(binding)
the binding thing here means that every local variable can be seen inside by ERB when it's rendered. (Technically, it's not just variables, but methods available in the scope, and some other things).
I would skip the txt file, and instead go with a yml file.
Check this site out for a little more info on how to do it: http://innovativethought.net/2009/01/02/making-configuration-files-with-yaml-revised/
I agree about YML. If you really want (or have) to use a text file, you can do something like that :
class MyClass
def init_variables(text)
text.scan(/(.*)=(.*)\n/).each do |couple|
instance_variable_set("#" + couple[0], couple[1])
end
end
end
my_obj = MyClass.new
my_obj.init_variables("header=foo\ncontent1=bar")
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 %>