From d4317e20e3525946c95d127c5970a84eb5cfdaa6 Mon Sep 17 00:00:00 2001
From: Cory Fauver
+ ```ruby
+ #
+ # config/routes.rb
+ #
+ Rails.application.routes.draw do
-```ruby
-#
-# config/routes.rb
-#
+ end
+ ```
+
+ ```ruby
+ #
+ # config/routes.rb
+ #
-Your routes tell your app how to direct **HTTP requests** to **controller actions**. Define your `root` and creatures `index` routes as follows:
+ Rails.application.routes.draw do
+ root "creatures#index"
-```ruby
-#
-# config/routes.rb
-#
+ get "/creatures", to: "creatures#index", as: "creatures"
-Rails.application.routes.draw do
- root "creatures#index"
+ end
+ ```
+
+ ```ruby
+ #
+ # app/controllers/creatures_controller.rb
+ #
+ class CreaturesController < ApplicationController
+ # display all creatures
+ def index
+ # get all creatures from db and save to instance variable
+ @creatures = Creature.all
+ # render the index view (it has access to instance variable)
+ render :index
+ end
end
+ ```
+
- Name: <%= creature.name %>
+ Name: <%= creature.name %>Hint: It should now look exactly like this...
+
-Rails.application.routes.draw do
+Your routes tell your app how to direct **HTTP requests** to **controller actions**. Define your `root` route and your creatures `index` route to refer to the index method in the creatures controller:
-end
-```
+Hint:
+ Hint:
+ Here's one way that could look:
- Description: <%= creature.description %>
+ ```html
+
+
+ <% @creatures.each do |creature| %>
+
+ Description: <%= creature.description %>
+
+ ```ruby + # + #/config/routes.rb + # -Rails.application.routes.draw do - root to: "creatures#index" + Rails.application.routes.draw do + root to: "creatures#index" - get "/creatures", to: "creatures#index" - get "/creatures/new", to: "creatures#new" -end -``` + get "/creatures", to: "creatures#index" + get "/creatures/new", to: "creatures#new" + end + ``` +
++ ```html + + + <%= form_for :creature, url: "/creatures", method: "post" do |f| %> + <%= f.text_field :name %> + <%= f.text_area :description %> + <%= f.submit "Save Creature" %> + <% end %> + ``` +
+
```ruby
#
@@ -240,22 +248,27 @@ The Rails convention is to make a form for new creatures at the `/creatures/new`
When a user sends a GET request to `/creatures/new`, your server will search for a `creatures#new` action, so you need to create a controller method to handle this request. `creatures#new` should render the view `new.html.erb` inside the `app/views/creatures` folder.
-```ruby
-#
-# app/controllers/creatures_controller.rb
-#
+
+ ```ruby
+ #
+ # app/controllers/creatures_controller.rb
+ #
-class CreaturesController < ApplicationController
+ class CreaturesController < ApplicationController
- ...
+ ...
- # show the new creature form
- def new
- render :new
- end
+ # show the new creature form
+ def new
+ render :new
+ end
-end
-```
+ end
+ ```
+
+
+ ```ruby
+ #
+ #/config/routes.rb
+ #
-Rails.application.routes.draw do
- root to: "creatures#index"
+ Rails.application.routes.draw do
+ root to: "creatures#index"
- get "/creatures", to: "creatures#index", as: "creatures"
- get "/creatures/new", to: "creatures#new", as: "new_creature"
- post "/creatures", to: "creatures#create"
+ get "/creatures", to: "creatures#index", as: "creatures"
+ get "/creatures/new", to: "creatures#new", as: "new_creature"
+ post "/creatures", to: "creatures#create"
-end
-```
+ end
+ ```
+
+ ```ruby
+ #
+ # app/controllers/creatures_controller.rb
+ #
-class CreaturesController < ApplicationController
+ class CreaturesController < ApplicationController
- ...
+ ...
- # create a new creature in the database
- def create
- # whitelist params and save them to a variable
- creature_params = params.require(:creature).permit(:name, :description)
+ # create a new creature in the database
+ def create
+ # whitelist params and save them to a variable
+ creature_params = params.require(:creature).permit(:name, :description)
- # create a new creature from `creature_params`
- creature = Creature.new(creature_params)
+ # create a new creature from `creature_params`
+ creature = Creature.new(creature_params)
- # if creature saves, redirect to route that displays all creatures
- if creature.save
- redirect_to creatures_path
- # redirect_to creatures_path is equivalent to:
- # redirect_to "/creatures"
- end
- end
+ # if creature saves, redirect to route that displays all creatures
+ if creature.save
+ redirect_to creatures_path
+ # redirect_to creatures_path is equivalent to:
+ # redirect_to "/creatures"
+ end
+ end
-end
-```
+ end
+ ```
+
+
+ ```ruby
+ #
+ # app/controllers/creatures_controller.rb
+ #
-class CreaturesController < ApplicationController
+ class CreaturesController < ApplicationController
- ...
+ ...
- # show the new creature form
- def new
- @creature = Creature.new
- render :new
- end
+ # show the new creature form
+ def new
+ @creature = Creature.new
+ render :new
+ end
-end
-```
+ end
+ ```
+
+ ```html
+
-<%= form_for @creature do |f| %>
- <%= f.text_field :name %>
- <%= f.text_area :description %>
- <%= f.submit "Save Creature" %>
-<% end %>
-```
+ <%= form_for @creature do |f| %>
+ <%= f.text_field :name %>
+ <%= f.text_area :description %>
+ <%= f.submit "Save Creature" %>
+ <% end %>
+ ```
+
+ ```ruby
+ #
+ # config/routes.rb
+ #
-Rails.application.routes.draw do
- root to: "creatures#index"
+ Rails.application.routes.draw do
+ root to: "creatures#index"
- get "/creatures", to: "creatures#index", as: "creatures"
- get "/creatures/new", to: "creatures#new", as: "new_creature"
- post "/creatures", to: "creatures#create"
- get "/creatures/:id", to: "creatures#show", as: "creature"
-end
-```
+ get "/creatures", to: "creatures#index", as: "creatures"
+ get "/creatures/new", to: "creatures#new", as: "new_creature"
+ post "/creatures", to: "creatures#create"
+ get "/creatures/:id", to: "creatures#show", as: "creature"
+ end
+ ```
+
-class CreaturesController < ApplicationController
+ ```ruby
+ #
+ # app/controllers/creatures_controller.rb
+ #
- ...
+ class CreaturesController < ApplicationController
- # display a specific creature
- def show
- # get the creature id from the url params
- creature_id = params[:id]
+ ...
- # use `creature_id` to find the creature in the database
- # and save it to an instance variable
- @creature = Creature.find_by_id(creature_id)
+ # display a specific creature
+ def show
+ # get the creature id from the url params
+ creature_id = params[:id]
- # render the show view (it has access to instance variable)
- render :show
- end
+ # use `creature_id` to find the creature in the database
+ # and save it to an instance variable
+ @creature = Creature.find_by_id(creature_id)
-end
-```
+ # render the show view (it has access to instance variable)
+ render :show
+ end
+
+ end
+ ```
+
+ ```html
+
- <%= @creature.description %> <%= @creature.description %>Hint:
+ Hint:
+ The code:
+ Hint:
+ It might look something like this:
+ Hint:
+ Hint:
+ It might look like this:
+ <%= @creature.name %>
-<%= @creature.name %>
+
+ ```ruby + # + # app/controllers/creatures_controller.rb + # -class CreaturesController < ApplicationController + class CreaturesController < ApplicationController - ... + ... - # create a new creature in the database - def create - # whitelist params and save them to a variable - creature_params = params.require(:creature).permit(:name, :description) + # create a new creature in the database + def create + # whitelist params and save them to a variable + creature_params = params.require(:creature).permit(:name, :description) - # create a new creature from `creature_params` - creature = Creature.new(creature_params) + # create a new creature from `creature_params` + creature = Creature.new(creature_params) - # if creature saves, redirect to route that displays - # ONLY the newly created creature - if creature.save - redirect_to creature_path(creature) - # redirect_to creature_path(creature) is equivalent to: - # redirect_to "/creatures/#{creature.id}" + # if creature saves, redirect to route that displays + # ONLY the newly created creature + if creature.save + redirect_to creature_path(creature) + # redirect_to creature_path(creature) is equivalent to: + # redirect_to "/creatures/#{creature.id}" + end end - end -end -``` + end + ``` +
++ ```ruby + # + # config/routes.rb + # -Rails.application.routes.draw do - root to: "creatures#index" + Rails.application.routes.draw do + root to: "creatures#index" - get "/creatures", to: "creatures#index", as: "creatures" - get "/creatures/new", to: "creatures#new", as: "new_creature" - post "/creatures", to: "creatures#create" - get "/creatures/:id", to: "creatures#show", as: "creature" - get "/creatures/:id/edit", to: "creatures#edit", as: "edit_creature" -end -``` + get "/creatures", to: "creatures#index", as: "creatures" + get "/creatures/new", to: "creatures#new", as: "new_creature" + post "/creatures", to: "creatures#create" + get "/creatures/:id", to: "creatures#show", as: "creature" + get "/creatures/:id/edit", to: "creatures#edit", as: "edit_creature" + end + ``` +
++ ```ruby + # + # app/controllers/creatures_controller.rb + # -class CreaturesController < ApplicationController + class CreaturesController < ApplicationController - ... + ... - # show the edit creature form - def edit - # get the creature id from the url params - creature_id = params[:id] + # show the edit creature form + def edit + # get the creature id from the url params + creature_id = params[:id] - # use `creature_id` to find the creature in the database - # and save it to an instance variable - @creature = Creature.find_by_id(creature_id) + # use `creature_id` to find the creature in the database + # and save it to an instance variable + @creature = Creature.find_by_id(creature_id) - # render the edit view (it has access to instance variable) - render :edit - end + # render the edit view (it has access to instance variable) + render :edit + end -end -``` + end + ``` +
++ ```html + -<%= form_for @creature do |f| %> - <%= f.text_field :name %> - <%= f.text_area :description %> - <%= f.submit "Save Creature" %> -<% end %> -``` + <%= form_for @creature do |f| %> + <%= f.text_field :name %> + <%= f.text_area :description %> + <%= f.submit "Save Creature" %> + <% end %> + ``` +
++ ```ruby + # + # config/routes.rb + # -Rails.application.routes.draw do - root to: "creatures#index" + Rails.application.routes.draw do + root to: "creatures#index" - get "/creatures", to: "creatures#index", as: "creatures" - get "/creatures/new", to: "creatures#new", as: "new_creature" - post "/creatures", to: "creatures#create" - get "/creatures/:id", to: "creatures#show", as: "creature" - get "/creatures/:id/edit", to: "creatures#edit", as: "edit_creature" - patch "/creatures/:id", to: "creatures#update" -end -``` + get "/creatures", to: "creatures#index", as: "creatures" + get "/creatures/new", to: "creatures#new", as: "new_creature" + post "/creatures", to: "creatures#create" + get "/creatures/:id", to: "creatures#show", as: "creature" + get "/creatures/:id/edit", to: "creatures#edit", as: "edit_creature" + patch "/creatures/:id", to: "creatures#update" + end + ``` +
+-class CreaturesController < ApplicationController + ```ruby + # + # app/controllers/creatures_controller.rb + # - ... + class CreaturesController < ApplicationController - # update a creature in the database - def update - # get the creature id from the url params - creature_id = params[:id] + ... - # use `creature_id` to find the creature in the database - # and save it to an instance variable - creature = Creature.find_by_id(creature_id) + # update a creature in the database + def update + # get the creature id from the url params + creature_id = params[:id] - # whitelist params and save them to a variable - creature_params = params.require(:creature).permit(:name, :description) + # use `creature_id` to find the creature in the database + # and save it to an instance variable + creature = Creature.find_by_id(creature_id) - # update the creature - creature.update_attributes(creature_params) + # whitelist params and save them to a variable + creature_params = params.require(:creature).permit(:name, :description) - # redirect to show page for the updated creature - redirect_to creature_path(creature) - # redirect_to creature_path(creature) is equivalent to: - # redirect_to "/creatures/#{creature.id}" - end + # update the creature + creature.update_attributes(creature_params) -end -``` + # redirect to show page for the updated creature + redirect_to creature_path(creature) + # redirect_to creature_path(creature) is equivalent to: + # redirect_to "/creatures/#{creature.id}" + end + + end + ``` +
+-Rails.application.routes.draw do - root to: "creatures#index" - - get "/creatures", to: "creatures#index", as: "creatures" - get "/creatures/new", to: "creatures#new", as: "new_creature" - post "/creatures", to: "creatures#create" - get "/creatures/:id", to: "creatures#show", as: "creature" - get "/creatures/:id/edit", to: "creatures#edit", as: "edit_creature" - patch "/creatures/:id", to: "creatures#update" - delete "/creatures/:id", to: "creatures#destroy" -end -``` + ```ruby + # + # config/routes.rb + # + + Rails.application.routes.draw do + root to: "creatures#index" + + get "/creatures", to: "creatures#index", as: "creatures" + get "/creatures/new", to: "creatures#new", as: "new_creature" + post "/creatures", to: "creatures#create" + get "/creatures/:id", to: "creatures#show", as: "creature" + get "/creatures/:id/edit", to: "creatures#edit", as: "edit_creature" + patch "/creatures/:id", to: "creatures#update" + delete "/creatures/:id", to: "creatures#destroy" + end + ``` +
++ ```ruby + # + # app/controllers/creatures_controller.rb + # -class CreaturesController < ApplicationController + class CreaturesController < ApplicationController - ... + ... - # delete a creature from the database - def destroy - # get the creature id from the url params - creature_id = params[:id] + # delete a creature from the database + def destroy + # get the creature id from the url params + creature_id = params[:id] - # use `creature_id` to find the creature in the database - # and save it to an instance variable - creature = Creature.find_by_id(creature_id) + # use `creature_id` to find the creature in the database + # and save it to an instance variable + creature = Creature.find_by_id(creature_id) - # destroy the creature - creature.destroy + # destroy the creature + creature.destroy - # redirect to creatures index - redirect_to creatures_path - # redirect_to creatures_path is equivalent to: - # redirect_to "/creatures" - end + # redirect to creatures index + redirect_to creatures_path + # redirect_to creatures_path is equivalent to: + # redirect_to "/creatures" + end -end -``` + end + ``` +
+```html @@ -663,6 +759,8 @@ Add a delete button to the view that displays a single creature:
<%= @creature.description %>
<%= button_to "Delete", @creature, method: :delete %> ``` + +- ```ruby - # - # app/controllers/creatures_controller.rb - # - class CreaturesController < ApplicationController + ```ruby + # + # app/controllers/creatures_controller.rb + # - ... + class CreaturesController < ApplicationController + + # ... - # create a new creature in the database - def create - # whitelist params and save them to a variable - creature_params = params.require(:creature).permit(:name, :description) + # create a new creature in the database + def create + # whitelist params and save them to a variable + creature_params = params.require(:creature).permit(:name, :description) - # create a new creature from `creature_params` - creature = Creature.new(creature_params) + # create a new creature from `creature_params` + creature = Creature.new(creature_params) - # if creature saves, redirect to route that displays all creatures - if creature.save - redirect_to creatures_path - # redirect_to creatures_path is equivalent to: - # redirect_to "/creatures" - end + # if creature saves, redirect to route that displays all creatures + if creature.save + redirect_to creatures_path + # redirect_to creatures_path is equivalent to: + # redirect_to "/creatures" end - end - ``` + end + ```