0
0
Ruby on Railsframework~10 mins

Action methods in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Action methods
HTTP Request Received
Router Matches URL
Controller Selected
Action Method Called
Action Executes Code
Render View or Redirect
HTTP Response Sent
When a web request comes in, Rails finds the right controller and calls the action method, which runs code and sends back a response.
Execution Sample
Ruby on Rails
class BooksController < ApplicationController
  def show
    @book = Book.find(params[:id])
  end
end
This action method finds a book by its ID and prepares it for the view to display.
Execution Table
StepActionCode ExecutedState ChangeOutput/Next Step
1Receive HTTP GET /books/3Router matches to BooksController#showController set to BooksControllerCall show action method
2Call show method@book = Book.find(params[:id])@book set to Book with id=3Prepare data for view
3Render viewRender show.html.erb with @bookView ready with @book dataSend HTTP response with HTML
4Response sentHTTP 200 OK with book detailsRequest completeEnd
💡 Action method finishes after rendering view or redirecting, response sent to client
Variable Tracker
VariableStartAfter Step 2Final
@booknilBook(id=3, title='Example')Book(id=3, title='Example')
Key Moments - 2 Insights
Why does the action method need to set instance variables like @book?
Instance variables like @book are used to pass data from the action method to the view template, as shown in step 2 and 3 of the execution_table.
What happens if the action method does not render or redirect?
Rails will try to render the default view matching the action name automatically after the action method finishes, as seen in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of @book after step 2?
ABook with id=3
Bnil
Cparams[:id]
DEmpty object
💡 Hint
Check the 'State Change' column in step 2 of the execution_table
At which step does Rails send the HTTP response back to the client?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look at the 'Output/Next Step' column for when the response is sent
If the action method did not set @book, what would happen in the view rendering step?
AView renders with empty @book
BRails skips rendering the view
CView raises an error for missing @book
DRails redirects automatically
💡 Hint
Instance variables must be set in the action method to be used in the view (see variable_tracker)
Concept Snapshot
Action methods are controller functions called by Rails when a matching URL is requested.
They run code to prepare data, usually setting instance variables.
After running, Rails renders a view or redirects.
Instance variables connect controller data to views.
If no render or redirect, Rails renders default view automatically.
Full Transcript
In Rails, when a web request comes in, the router finds the right controller and action method. The action method runs code, often setting instance variables like @book to pass data to the view. After the action runs, Rails renders the matching view template or redirects. The HTTP response is then sent back to the client. This flow ensures the right data is shown on the page. If the action method does not explicitly render or redirect, Rails automatically renders the default view for that action. Instance variables are key to sharing data between the controller and the view.