The public and assets folders help organize files that your Rails app sends directly to the browser, like images, styles, and scripts.
Public and assets folders in Ruby on Rails
app/assets/ stylesheets/ javascripts/ images/ public/ favicon.ico robots.txt other static files
The app/assets folder holds files processed by Rails before sending to the browser.
The public folder holds static files served as-is without processing.
app/assets/stylesheets/application.css app/assets/javascripts/application.js app/assets/images/logo.png
public/robots.txt
public/favicon.ico
public/404.htmlThis example shows how CSS in app/assets is linked in views and how static files in public are served directly.
# Folder structure example # app/assets/stylesheets/custom.css body { background-color: #f0f0f0; } # public/robots.txt User-agent: * Disallow: # In a Rails view (app/views/layouts/application.html.erb): # <%= stylesheet_link_tag 'custom', media: 'all' %> # When you start the Rails server and visit the site: # - The custom.css styles apply to the page. # - The robots.txt file is accessible at /robots.txt
Files in app/assets are processed by Rails (like minifying CSS/JS).
Files in public are served exactly as they are, no processing.
Use app/assets for files that change often or need preprocessing.
The public folder holds static files served directly.
The assets folder holds files Rails processes before sending.
Use these folders to organize images, styles, scripts, and static files for your app.
Practice
public folder in a Rails application?Solution
Step 1: Understand the role of the public folder
Thepublicfolder contains files like images, HTML, or JavaScript that Rails serves directly to the browser without any processing.Step 2: Compare with other folders
Theassetsfolder is for files Rails processes (like compiling CSS or JavaScript), so it is not the public folder's role.Final Answer:
To store static files served directly without processing -> Option AQuick Check:
Public folder = static files served directly [OK]
- Confusing public with assets folder
- Thinking public holds Ruby code
- Assuming public processes files before serving
Solution
Step 1: Identify the standard assets folder for stylesheets
Rails expects CSS files to be insideapp/assets/stylesheetsfor precompilation and processing.Step 2: Eliminate incorrect paths
Thepublicfolder is for static files, not processed assets. Theconfigfolder does not hold assets.Final Answer:
app/assets/stylesheets/style.css -> Option CQuick Check:
CSS in assets/stylesheets = app/assets/stylesheets/style.css [OK]
- Placing CSS directly in public folder
- Using wrong folder like config/assets
- Confusing assets with public folder paths
logo.png placed inside public/images, what URL path will serve this image in a Rails app?Solution
Step 1: Understand how public folder maps to URL
Files insidepublicare served from the root URL path. Sopublic/images/logo.pngis accessible at/images/logo.png.Step 2: Eliminate other options
/assets/images/logo.pngis for assets pipeline files, not public./public/images/logo.pngand/app/assets/images/logo.pngare not valid URL paths.Final Answer:
/images/logo.png -> Option DQuick Check:
Public folder maps directly to root URL [OK]
- Using /assets prefix for public files
- Including 'public' in URL path
- Confusing assets and public URL paths
app/assets/javascripts but it is not loading in your Rails app. What is a likely cause?Solution
Step 1: Check how assets are loaded in Rails
Files inapp/assets/javascriptsneed to be referenced in your layout via a manifest or pack tag to load properly.Step 2: Eliminate incorrect causes
JavaScript files do not belong inpublicfor processing. Rails fully supports JavaScript in assets folder.Final Answer:
You forgot to include the JavaScript pack or manifest in your layout -> Option AQuick Check:
Assets must be linked in layout to load [OK]
- Expecting assets to load without linking in layout
- Placing JS files in wrong folders
- Thinking public folder is for processed assets
Solution
Step 1: Choose folder for instant loading without processing
Files inpublicare served directly, so placing favicon.ico inpublicroot ensures instant loading.Step 2: Reference favicon correctly in HTML
Use a standard HTML<link>tag with href pointing to/favicon.icofor browsers to find it.Final Answer:
Place in public folder root and use <link rel='shortcut icon' href='/favicon.ico'> -> Option BQuick Check:
Favicon in public root + direct link = Place inpublicfolder root and use<link rel='shortcut icon' href='/favicon.ico'>[OK]
- Placing favicon in assets and expecting instant load
- Using image_tag instead of link tag for favicon
- Referencing wrong URL path for favicon
Ready to prove it?
Pass the test and this pattern moves unknown
