09 Views and Controllers
context.rb --take the subcontext and make them into one context
CRUD Operations¶
![[Screen Shot 2024-02-13 at 11.24.34.png]]
RESTful Actions: ![[Pasted image 20240311234018.png]]
7 controller actions --need a route to the controller action
- GET /posts for index
- GET /posts/new for new
- POST /posts for create
- GET /posts/:id for show
- GET /posts/:id/edit for edit
- PATCH/PUT /posts/:id for update
- DELETE /posts/:id for destroy
- dispatcher -- routes table -- name a controller -- controller#action 比如 owner#index
- In
app/controller.rbfile, we define theindexaction:
Routes and URL Patterns¶
- In a Rails application, routes define the URL patterns that your application can respond to.
- They map incoming requests to controllers and actions.
HTTP Verbs and Actions¶
- GET: This HTTP method is used to request data from a specified resource.
- In the context of Rails routes, using
getas a shortcut allows you to define a route that handles GET requests, typically to read or retrieve a resource.
- In the context of Rails routes, using
- PUT and PATCH: Both are used for updating resources.
- In Rails,
putis traditionally used to update a resource entirely, whilepatchis for partially updating it. Modern Rails applications usepatchmore commonly for updates.
- In Rails,
- Redirects and Rendering: Redirecting to a different page (
redirect_to @owner) or going back to a previous page as part of the control flow in a Rails controller. This is often used after a database operation like insert, update, or delete (reflected in SQL terms) to navigate users to the appropriate view based on the outcome of their request.
SQL Operations and Rails¶
- While SQL directly uses
INSERT,UPDATE, andDELETEcommands to modify data, Rails abstracts these into methods like.save,.update, and.destroy. - You don't see SQL directly; you see its effects through changes in the database, much like how routes abstract the details of HTTP requests and responses.
Part of a Controller action¶
if ()
redirect to some place
redirect_to @owner -- shortcut of owner_path(@owner)
else
go back to the new page
- If the condition is true, it redirects to the
@ownerresource, using a Rails path helper (owner_path(@owner)as a shortcut). - If the condition is false, it suggests going back to the 'new' page, likely to correct form inputs. This pattern is common in create or update actions where a form submission's success leads to a redirect, and failure re-renders the form.
get a shortcut -- use the GET verb read something - create a shortcut to get this path 最右边那一栏就是shortcut
show.html.erb¶
- Location: typically found in the
app/views/model_namedirectory, wheremodel_nameis the name of the model associated with the controller - Def: a template used to display the details of a specific instance of a model
- The
.html.erbextension indicates that it is an HTML file that can contain embedded Ruby code (ERB). - The
show.html.erbfile is part of the view layer in the MVC (Model-View-Controller) architecture pattern used by Rails. The controller fetches the model data, passes it to the view, and then the view uses this data to render the HTML page to the user. - render partial: "something" 有时会省略partial这个词 默认就是partial
routes.rb¶
- def: file that defines how your application maps incoming HTTP requests to specific controllers and actions
- E.g., create the routes for get
- example: Blog application
PostsControllerwith actionsindex(to list all posts)show(to show a specific post)new(to display a form for creating a new post)create(to save the new post)edit(to edit a post)update(to update a post)
routes.rb:- This simple line,
resources :posts, tells Rails to create seven different routes in your application, all mapping to thePostsController. These routes correspond to the standard CRUD (Create, Read, Update, Delete) operations, including:GET /postsforindexGET /posts/newfornewPOST /postsforcreateGET /posts/:idforshowGET /posts/:id/editforeditPATCH/PUT /posts/:idforupdateDELETE /posts/:idfordestroy
- This simple line,
Basic Structure of routes.rb¶
Rails.application.routes.draw do
# Defines the root path route ("/")
root "pages#home"
# Example of a regular route
get 'about', to: 'pages#about', as: 'about'
# Example of a resource route (maps HTTP verbs to controller actions automatically)
resources :articles
# Example of a nested resource
resources :articles do
resources :comments
end
# Example of a namespace route
namespace :admin do
resources :articles
end
# Example of a custom named route
get 'login', to: 'sessions#new', as: :login
# Direct routes to a specified path
direct(:homepage) { "http://www.example.com" }
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'welcome#index'
end
Step by Step Explanation¶
-
Root Route:
root "pages#home"sets the root route of your application, which is the default page displayed when someone visits the base URL of your site. Here, it routes to thehomeaction in thepagescontroller. -
Regular Route:
get 'about', to: 'pages#about', as: 'about'defines a simple, custom route. It maps a GET request to/aboutto theaboutaction in thepagescontroller. Theas: 'about'part creates a URL helper namedabout_pathwhich can be used in your views. -
Resource Route:
resources :articlescreates RESTful routes for thearticlesresource, automatically mapping standard HTTP verbs (GET, POST, PUT/PATCH, DELETE) to corresponding actions (index,show,new,create,edit,update,destroy) in thearticlescontroller. -
Nested Resource: The nested resources syntax allows you to express relationships between resources. In the example,
resources :articles do resources :comments endcreates routes for comments that are nested within articles, reflecting the hierarchical relationship between articles and their comments. -
Namespace Route:
namespace :admin do resources :articles endgroups routes under anadminnamespace, segregating them from the rest of your application routes. This is useful for organizing and restricting parts of your application, like an admin panel. -
Custom Named Route:
get 'login', to: 'sessions#new', as: :loginmaps a GET request to/loginto thenewaction in thesessionscontroller, creating alogin_pathURL helper. This is useful for custom authentication flows. -
Direct Routes:
direct(:homepage) { "http://www.example.com" }provides a way to create custom URL helpers directly, without needing an associated route within the application. -
Root Route Redefined: Although having multiple root definitions is not typical and can be confusing, the last line,
root :to => 'welcome#index', shows another way of defining the root route, overriding any previous root definition.
What It Does¶
-
Routing Requests: The
routes.rbfile maps URLs to controllers and actions. When a request comes in, Rails uses this file to determine which controller and action to send the request to. -
URL Helpers: It also defines URL helpers (like
about_pathorlogin_path) that you can use throughout your application to generate URLs. This makes your views and controllers cleaner and more maintainable. -
Resourceful Routing: By using resourceful routing, Rails encourages RESTful design, making it easier to create consistent and predictable URLs for interacting with resources.
Semi-static Page Routes¶
These routes are likely intended for pages that rarely change. They are prefixed with home/ and directed to various actions within the home controller:
get 'home', to: 'home#index', as: :homeroutes to the main homepage of the application.- The following routes (
about,contact,privacy,search) are similarly structured, directing to their respective actions within thehomecontroller and named for easy reference (as: :about,as: :contact, etc.).
Authentication Routes¶
This section deals with user authentication and session management:
resources :sessionsandresources :userscreate the full set of RESTful routes for sessions and users, respectively, facilitating actions like creating, viewing, editing, and deleting user sessions and user profiles.- Additional specific routes for user signup (
users/new), editing the current user's profile (user/edit), logging in (login), and logging out (logout) provide clear, named paths for these common authentication actions.
Resource Routes¶
These routes use the resources method to automatically generate RESTful routes for various models in the application:
- Models such as
owners,animals,pets,visits,dosages,treatments,medicines, andproceduresare all set up with routes that map HTTP verbs to controller actions, covering the standard CRUD (Create, Read, Update, Delete) operations and more.
Routes for Medicine and Procedure Costs¶
These routes handle the creation of new medicine and procedure costs:
getroutes formedicine_costs/newandprocedure_costs/newprovide forms to create new costs.postroutes formedicine_costsandprocedure_costshandle the form submission to actually create the costs in the database.
Other Custom Routes¶
- An example of a custom route is
get 'visits/:id/dosages', to: 'visits#dosages', as: :visit_dosages, which likely shows all dosages for a specific visit. The:idsegment captures the visit's ID from the URL.
Commented-Out Routes for Searching¶
These commented-out routes (medicines/search, owners/search, pets/search) suggest that the application had or planned to have search functionality for medicines, owners, and pets, but these routes are currently not in use.
Root Route¶
root 'home#index'sets the application's root path to theindexaction of thehomecontroller, meaning this is the default page shown when visiting the base URL of the application.- In the provided
routes.rbfile, thehome_pathis defined by this line:get 'home', to: 'home#index', as: :home
Breaking this down:
- HTTP Verb (
get): Specifies that this route responds to HTTP GET requests. - Path (
'home'): The URL path that the route matches. In this case, it matches the/homeURL. - Controller and Action (
to: 'home#index'): This part specifies that theindexaction of thehomecontroller should handle the request. Thehomecontroller is likely responsible for rendering the application's homepage or a similar landing page. - Named Route (
as: :home): This creates a named route helper,home_path. In your views and controllers, you can usehome_pathto generate the URL/homeprogrammatically, ensuring that your code does not break if the URL structure changes later.