Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8573497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:15:52+00:00 2026-06-11T19:15:52+00:00

I am completely new to Rails and I’m trying to make each filetype that

  • 0

I am completely new to Rails and I’m trying to make each filetype that is in a directory have its own icon, i can only get it to show one please help? Here is what I have so far.

Controller:

class DocsController < ApplicationController
 def port
   @files = Dir.glob("public/folder/*")

   filetype = [".pdf", ".txt"]

   if filetype.include? ".pdf"
    @extension = "pdf.png"
   elsif filetype.include? ".txt"
    @extension = "text.png"
   else
    @extension = "folder.png"
   end
 end
end

View:

<% @files.each do |file| %>
<div class="filediv">
    <%= image_tag @extension, :size => "150x150" %>
    <p><%= file.gsub("public/folder/", "") %></p>
</div>
<% end %>

This is resulting in everything having the pdf icon, can someone tell me what i am doing wrong?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:15:53+00:00Added an answer on June 11, 2026 at 7:15 pm

    I think you need to add helper method

    def extension_image(file)
      ext =File.extname(file)
      if ext==".pdf"
        "pdf.png"
      elsif ext == ".txt"
       "text.png"
      else
        "folder.png"
      end
    end
    
    
     <%= image_tag extension_image(file), :size => "150x150" %>
    

    filetype = [“.pdf”, “.txt”]

    remove this code.

    if filetype.include? ".pdf"
        @extension = "pdf.png"
       elsif filetype.include? ".txt"
        @extension = "text.png"
       else
        @extension = "folder.png"
       end
    end
    

    Now why is always displays a pdf extension
    it’s simple

    [1,2,3].include?(1)  it's always true so no further checking 
    

    Checkout how include works in array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to rails and playing with the code to make pages
I'm a completely new rails developer so bear with me :-) I have two
I'm completely new to Ruby on Rails, have never worked on it, but I
I'm completely new to rails and I'm having a lot of trouble getting my
Complete new person to Ruby and Rails here... Have tried some tutorials in the
Completely new to java and I have been playing around with regex in a
I am completely new to HTML5 and have been reading about it for the
I'm completely new to javascript, but I'm trying to create a beta form for
I'm completely new to Rails, but I've done quite a bit of searching and
I'm completely new to Rails and just installed Rails 3 on Ubuntu 10.04. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.