0
0
Ruby on Railsframework~10 mins

Public and assets folders in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Public and assets folders
Request from browser
Rails server receives request
Check if file exists in /public
Serve static file
Send file to browser
Response sent to browser
When a browser requests a file, Rails first looks in the public folder for static files. If not found, it checks the assets folder to compile and serve CSS, JS, or images.
Execution Sample
Ruby on Rails
# Browser requests /assets/logo.png
# Rails checks /public/assets/logo.png
# If found, serves directly
# Else, compiles from /app/assets/images/logo.png
# Sends file back to browser
This shows how Rails serves static files from public or compiles assets from the assets folder.
Execution Table
StepActionFile CheckedResultResponse to Browser
1Browser requests /assets/logo.png--Waiting
2Rails checks /public/assets/logo.png/public/assets/logo.pngFile existsServe static file
3Send file to browser--Browser receives /assets/logo.png
4Request complete--Response finished
💡 File found in /public, served directly without asset pipeline
Variable Tracker
VariableStartAfter Step 2After Step 3Final
requested_path/assets/logo.png/assets/logo.png/assets/logo.png/assets/logo.png
file_locationunknown/public/assets/logo.png/public/assets/logo.png/public/assets/logo.png
response_statuswaitingready to sendsentcompleted
Key Moments - 2 Insights
Why does Rails serve files directly from the public folder without compiling?
Because files in /public are static and ready to serve, Rails skips asset compilation for faster response, as shown in execution_table step 2.
What happens if the requested file is not in the public folder?
Rails then looks in the assets folder to compile the file before serving, but this is not shown here because the file was found in /public.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what file path does Rails check first when a request comes in?
A/public/assets/logo.png
B/app/assets/images/logo.png
C/config/routes.rb
D/tmp/cache/assets
💡 Hint
Check Step 2 in the execution_table where Rails checks the file location
At which step does Rails send the file back to the browser?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Response to Browser column in the execution_table
If the file was not found in /public, what would Rails do next?
AReturn 404 error immediately
BCheck and compile from assets folder
CRestart the server
DIgnore the request
💡 Hint
Refer to the concept_flow where No leads to checking the asset pipeline
Concept Snapshot
Rails serves static files from the /public folder first.
If not found, it compiles assets from /app/assets.
Public files are served directly for speed.
Assets folder files go through the asset pipeline.
This ensures efficient delivery of images, CSS, and JS.
Requests flow: browser -> public -> assets -> response.
Full Transcript
When a browser requests a file, Rails first looks in the public folder for that file. If the file exists there, Rails serves it directly to the browser without extra work. This makes serving static files fast and simple. If the file is not in the public folder, Rails then checks the assets folder. Files in assets are processed by the asset pipeline, which compiles and prepares them before sending. This process ensures that CSS, JavaScript, and images are optimized. The flow is: browser sends request, Rails checks public folder, if found serves file, else compiles from assets, then sends response. This helps Rails efficiently handle static and dynamic assets.