You always define your actions in the controller. RESTful routes are defined in config/routes.rb
. The model and view do not have anything to do with RESTful actions.
With respect to CRUD, POST is used to create, GET is used to read, PUT/PATCH are used to update, and DELETE is used to delete.
We use the method resources
in the config/routes.rb
file.
The model is the argument for the require
method, and the whitelisted attributes are the arguments for the permit
method. All arguments are symbols.
.all
is a class method, and can only be called on the model, which is singular and capitalized. .find(1..100)
will only return 100 instances, and .where(title: nil)
will return all posts with no title assigned.
We need the %
sign on both sides to signify ruby code within the tag. We only need a =
on the left side to render the return value of the contents within the tag.
We need to find the post associated with a given id. The argument of the .find
method is the id, which is an integer.
Routes are defined as Controller#Action
.
There are 7 RESTful actions: index, create, new, edit, show, update, and destroy.
<%= render 'form' %>
. What will this line do?Partials are always saved with an underscore in front of the partial name. When a partial is called in the view, if only the name of the partial is written (no folder name in front), the partial rendered will be in the same folder as the view. Rendering a partials does not redirect a user to a new page. It just loads view code from a separate file.
Partials are used so that developers don't have to repeat view code. If a piece of view code is used on multiple pages, then using a partial is a good idea. A nav bar or a form used in multiple places are both scenarios where a piece of view code is repeated. The other two options do not have view code that is repeated.
All the above actions need to be taken to create a comment that is associated with a post. Adding a nested route or a form for creation is not strictly necessary because you can create the association via rails console.
/houses/:house_id/rooms/new
to create a room that is associated with the house with ID :house_id
. Which of the following is valid code for the 'create' method in the rooms controller? Please select all that apply.# Finds correct house
@house = House.find(params[:house_id])
# Builds room associated with house with room parameters
@room = @house.rooms.build(room_params)
# Saves the room
@room.save
# Even though the other two work, this is the best way to do it.
# AND
# Finds the correct house, and creates a room associated with the house.
# Create is equivalent to build and save.
@house = House.find(params[:house_id]).rooms.create(room_params)
# AND
# Finds correct house
@house = House.find(params[:house_id])
# Creates a room, not associated with any house.
@room = Room.create(room_params)
# Manually add in association by adding the newly created room to the collection of rooms
# associated with the the correct house.
@house.rooms << @room
<%= form_for([@article, @article.comments.build]) do |f| %>
@article.comments.build
always creates a new comment, and adds in the association between the two models automatically. The last element in the list of the form helper is the model that is being created.
resources :restaurants do
resources :menus
end
/restaurants/:restaurant_id/menus/:id
This is what we call a nested resource, where :menus
are nested within :restaurants
. That means menus will come after restaurants in the URLs.
rake db:seed
executes any code that lives within the seeds.rb
file, which usually creates instances of existing models. rake db:migrate
changes the structure of the schema, and the structure of your database indirectly.
rake db:migrate
changes the structure, not the contents! To change the contents, you can add to your seeds.rb
file and run rake db:seed
, add instances via the rails console, or create new instances of models in the controller.
artist_album GET /artists/:artist_id/albums/:id albums#show
How would I create a link to the route above on a page that displays all albums, assuming @artist and @album are defined in the Albums controller under the appropriate method?<%= link_to 'show', artist_album_path(@artist, @album) %>
Take the portion under the PREFIX column of rake routes
and append _path
to the link helper. artist_album_path
is your URL helper. As arguments to the URL helper, pass in objects in the order they appear in the URL. In this case, pass in the artist instance first, and then the album instance.
Creating a Post model will automatically generate a new migration. Deleting/adding columns and deleting/adding tables also have to do with changing the structure of the schema. Adding a new comment into your database changes the contents and not the structure.