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 6763901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:31:53+00:00 2026-05-26T14:31:53+00:00

I am trying to choose an image randomly from a sub directory inside my

  • 0

I am trying to choose an image randomly from a sub directory inside my /app/assets/images directory using the Dir.glob() command, and then display it with an image_tag. Somehow I can’t get it to work.

Here’s my code:

- @badges = Dir.glob("app/assets/images/badges/*")
= image_tag @badges.sample

Which produces the following error:

ActionController::RoutingError (No route matches [GET] "/assets/app/assets/images/badges/produce.png"):

As you can see the asset pipeline is inserting an “/assets” in front of the directory. Alright Rails, I’ll meet you halfway here. So next I try removing /app/assets from the query path to make it work and get the following result:

- @badges = Dir.glob("images/badges/*")
  = image_tag @badges.sample

ActionController::RoutingError (No route matches [GET] "/assets"):

What am I doing wrong here? Thanks in advance for your help!

  • 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-05-26T14:31:54+00:00Added an answer on May 26, 2026 at 2:31 pm

    Dir.glob is going to return images with a relative path, so your produce.png file will be returned as:

    `app/assets/images/badges/produce.png`
    

    However, you need to pass only the badges/produce.png part to image_tag. You need to remove the stuff before this:

    = image_tag @badges.sample.gsub("app/assets/images/", "")
    

    You may want to stick this in a helper instead:

    def random_badge
      badges = Dir.glob("app/assets/images/badges/*")
      image_tag badges.sample.gsub("app/assets/images/", "")
    end
    

    and then in your view:

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

Sidebar

Related Questions

What am I trying to implement? A gallery of images using ViewPager. I choose
I'm building an app that randomly chooses and displays two images from bunch of
Hi I've been trying to fade from one image to another using trident java
I am trying to allow a user to select an image, either from the
I'm trying to get started with Amazon's EC2. I have to choose an image
I'm trying to filter through some images. Each image has 3 sets of values
I'm trying to add uploaded files from a directory to a form element in
I'm trying to upload an image file to my application, the user can choose
I'm making a map creator that chooses images randomly from various sets. What I'm
I'm trying to make a simple image browser for TinyMCE which I am using

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.