The public and assets folders help organize files that your Rails app sends directly to the browser, like images, styles, and scripts.
0
0
Public and assets folders in Ruby on Rails
Introduction
You want to add a logo image that everyone visiting your site can see.
You need to include custom CSS or JavaScript files for styling or interactivity.
You want to serve static files like robots.txt or favicon.ico.
You want to organize files that don't change often and don't need server processing.
You want to improve page load speed by serving precompiled assets.
Syntax
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.
Examples
Place CSS, JS, and images here to be compiled and served by Rails.
Ruby on Rails
app/assets/stylesheets/application.css app/assets/javascripts/application.js app/assets/images/logo.png
Place static files here that Rails serves directly without changes.
Ruby on Rails
public/robots.txt
public/favicon.ico
public/404.htmlSample Program
This example shows how CSS in app/assets is linked in views and how static files in public are served directly.
Ruby on Rails
# 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
OutputSuccess
Important Notes
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.
Summary
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.