CSC/ECE 517 Fall 2012/ch1b 1w64 nn
SaaS - 3.12 Controller and views
Introduction
Adding a new controller action to Rails application
- Create route in config/routes.rb if needed. Make sure that there is a route that is going to eventually route action, if not create one in routes.rb.
- Add the action method) in the appropriate app/controllers/*_controller.rb. You have to add the actual code in other words, the method in the appropriate controler file that will do whatever that action is.
- Ensure there is something for the action to render in app/views/model/action.html.haml. You have to assure that there is something to the action can render what its all done. Every trip through the controller has to end with returning something and will see that although the most common thing is returning a view whose name matches the controller action, we can also return other things.
MVC responsibilities
- Model: methods to get/manipulate data. Different methods are provided by active record to do that.
Movie.where(...), Movie.find(...)
- Controller: get data from Model, make available to view
<code> def show @movie = Movie.find(params[:id]) end </code>
params which is the parameter from the request that work hopefully parsed out for us by the routing subsystem. Let us assume that it is going to contain the id of the movie that we want to find beacause then we can just call movie.find. This is unsafe because in a real application because if params[:id] happens to be a non-existing thing, we will get an error. So in practice, we either use find by id or use exceptions. The instance variables in the controller are going to be available to the view
- View: display data, allow user interaction. In this example Show is going to display the details of a movie (description, rating). To select the view for rendering step, the default action the rails will take is, it will try to find a view whose directory name matches the name of the class and finally matches the name of the action.
URI helpers
Once the user is satisfied looking at page, what does he do? There is no place to click. Also how exactly does the user get to this page?
Helper method | URI returned | RESTful route | Action |
---|---|---|---|
movies_path | /movies | GET /movies | index |
movies_path | /movies | POST /movies | create |
new_movie_path | /movies/new | GET /movies/new | new |
edit_movie_path(m) | /movies/1/edit | GET /movies/:id/edit | edit |
movie_path(m) | /movies/1 | GET /movies/:id | show |
movie_path(m) | /movies/1 | PUT /movies/:id | update |
movie_path(m) | /movies/1 | DELETE /movies/:id | destroy |
Just like the routing subsystem allows us to map http methods and URI's to different control actions, the same routing subsystem also sets up these helpful methods that will generate the routes in the context of the page. Example when we are looking at the list of all movies index.index.html.haml, somewhere on that page there will be a link whose argument is movie_path with an argument.
link_to movie_path(3)
movie_path is the helper that set up and gives back a route to the show action for that movie id. So when the user clicks on it, the URI that is going to be generated is going to be movie/:id with a GET method because its a link. This is how it looks in the actual html.
<a href="/movies/3">...</a>
From here, once the URI is hit, that is going to get looked up in the routing subsystem. /movies/something matches the show action of the movies controller and it will take the wild card thing :id and puts that in params[:id]
GET /movies/:id {:action=>"show", :controller=>"movies"} params[:id]
With that appropriate controller action is called
def show @movie = Movie.find(params[:id]) end
Here the controller action can reasonably expect to find that params[:id] will be whatever this route said that it should match.
What else can we do?
We can let the user return a list of all movies. We can use RESTful URI helpers. Here it is the index action. movies_path with no arguments links to the index action. The line to add on show template is
link_to 'Back to List', movies_path
Back to List is the text that is click-able and movies_path is a method call. As the app gets more complicate, these mechanism scale very nicely