Rails Controllers - Request Processing
In the last lecture we say how the dispatcher routes a request to a
particular controller action (method).
The HTTP GET request http://localhost:3000/posts/1will route to the show method in the
params[:id]with a value of 1 to the controller. Note: this class is defined in the file:
show method will use the
to retrieve the post with
id=1 from the database, and assign it to the
Finally, the controller will pass @post to the view, i.e., to the
./app/views/posts/show.html.erband this will be used to create the HTML that will be sent to the
PostsController#show method is defined as follows:
# GET /posts/1 # GET /posts/1.json def show endIt doesn’t retrieve the post!
The desired post is actually retrieved from the database using a filter called
Filters allow controllers to run shared pre and post processing code over their methods.
In general, the “state” of an application which needs to persist across requests should be stored in the database. E.g., posts and comments are persisted in the database.
There are times when data needs to be persisted differently. E.g., the current contents of a shopping cart.
Whenever a user connects to a Rails application, a session is created.
Session data is stored in Rails using a hash structure that persists across requests, and can be accessed by controllers. Ex. session[:current_user] = user.id
A flash hash is part of the session that is cleared with each request (its
value is made to the next request). A controller can use this to send a
message that can be displayed to the user on the next request.
lstinlineflash[: notice] =0 Postwassuccessfullycreated.
Response - Rendering HTML or JSON
assumes that HTML will be returned. I.e., it’s the same as:
Rails can return other formats, e.g., JSON capabilities are also
provided by default.
Ex. The following request, will be routed to the same controller method as before:
However, it will be rendered using the file:
./app/views/posts/show.json.builderand JSON will be returned to the client.
One last look at rake routes:
Prefix Verb URI Pattern Controller#Action posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy
Rails may also respond to an HTTP request using the
This method actually tells the browser to send a new request for a
Rails has shortcuts for URLs within your application – they’re the
prefix listed when you view routes:
redirect_to posts_urlwill redirect to the index method in the PostsController.
You can assign a flash message as a part of a redirection: redirect_to @post, notice: ’Post was successfully created.’
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync