From d4317e20e3525946c95d127c5970a84eb5cfdaa6 Mon Sep 17 00:00:00 2001 From: Cory Fauver Date: Fri, 29 Apr 2016 10:58:49 -0700 Subject: [PATCH 1/4] detail/summaries in readme --- README.md | 165 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 96 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 94e7ee7e..b17f0dca 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ | Review **CRUD** in the context of a Rails application, especially **update** and **delete**. | | Implement **form helpers** in a Rails application. | -Researchers are collecting data on a local bog and need an app to quickly record field data. Your goal is to create a **Bog App**. If you get stuck at any point, feel free to reference the solution branch. +Researchers are collecting data on a local bog and need an app to quickly record field data. Your goal is to create a **Bog App**. If you get stuck at any point, feel free to reference the [solution branch](https://github.com/sf-wdi-25/rails_bog_app/tree/solution). ## Background @@ -37,13 +37,10 @@ REST stands for **REpresentational State Transfer**. We will strictly adhere to #### 1. Set up a new Rails project -Fork this repo, and clone it into your `develop` folder on your local machine. Change directories into `rails_bog_app`, and create a new Rails project: +Fork this repo, and clone it into your `wdi` folder on your local machine. Change directories into `rails-bog-app`, and create a new Rails project: ```zsh -➜ rails new bog_app -T -➜ mv bog_app/* ./ -➜ rm -rf bog_app -➜ rm README.rdoc +➜ rails new bog_app -T postgresql ➜ rake db:create ➜ rails s ``` @@ -78,32 +75,43 @@ To include the Bootstrap file you just downloaded, require it in `app/assets/sty #### 3. Define the `root` and creatures `index` routes -In Sublime, open up `config/routes.rb`. Inside the routes `draw` block, erase all the commented text. It should now look exactly like this: +In Atom, open up `config/routes.rb`. Inside the routes `draw` block, erase all the commented text. +
+ Hint: It should now look exactly like this... +

+ ```ruby + # + # config/routes.rb + # + Rails.application.routes.draw do -```ruby -# -# config/routes.rb -# + end + ``` +

+
+
-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: +

+ ```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 + ``` +

+
- get "/creatures", to: "creatures#index", as: "creatures" -end -``` In the Terminal, running `rake routes` will list all your routes. You'll see that some routes have a "prefix" listed. These routes have associated route helpers, which are methods Rails creates to generate URLs. The format of a route helper is `prefix_path`. For example, `creatures_path` is the full route helper for `GET /creatures` (the creatures index). We often use route helpers to generate URLs in forms, links, and controllers. @@ -115,26 +123,28 @@ Run the following command in the Terminal to generate a controller for creatures ➜ rails g controller creatures ``` -Next, define the `creatures#index` action in the creatures controller: +Next, define the `creatures#index` action in the creatures controller. The variable `@creatures` should be all of the creatures in the db: -```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 +
+ Hint: +

+ ```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 + ``` +

+
-end -``` #### 5. Set up the creature model @@ -163,7 +173,7 @@ irb(main):001:0> Creature.create({name: "Yoda", description: "Little green man"} When you create an application in development, you typically want some mock data to play with. In Rails, you can just drop this into the `db/seeds.rb` file. -Back in Sublime, add some seed data to `db/seeds.rb`: +Back in Atom, add some seed data to `db/seeds.rb`: ```ruby # @@ -182,16 +192,22 @@ If you look inside the `app/views` directory, the `/creatures` folder has alread Inside your creatures index view, iterate through all the creatures in the database, and display them on the page: -```html - - -<% @creatures.each do |creature| %> +
+ Here's one way that could look:

- Name: <%= creature.name %>
- Description: <%= creature.description %> + ```html + + + <% @creatures.each do |creature| %> +

+ Name: <%= creature.name %>
+ Description: <%= creature.description %> +

+ <% end %> + ```

-<% end %> -``` +
+
Go to `localhost:3000` in the browser. What do you see on the page? If you haven't already, `git add` and `git commit` the work you've done so far. @@ -201,18 +217,24 @@ Go to `localhost:3000` in the browser. What do you see on the page? If you haven The Rails convention is to make a form for new creatures at the `/creatures/new` path in our browser. -```ruby -# -#/config/routes.rb -# +
+ Hint: +

+ ```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 + ``` +

+
+
#### 2. Set up the creatures `new` action @@ -239,15 +261,20 @@ end Create the view `new.html.erb` inside the `app/views/creatures` folder. On this view, users should see a form to create new creatures in the database. -```html - - -<%= form_for :creature, url: "/creatures", method: "post" do |f| %> - <%= f.text_field :name %> - <%= f.text_area :description %> - <%= f.submit "Save Creature" %> -<% end %> -``` +
+ Here's one way that could look: +

+ ```html + + + <%= form_for :creature, url: "/creatures", method: "post" do |f| %> + <%= f.text_field :name %> + <%= f.text_area :description %> + <%= f.submit "Save Creature" %> + <% end %> + ``` +

+
**Note:** The URL you're submitting the form to is `/creatures` because it's the database collection for creatures, and the method is `post` because you're *creating* a new creature. From ec4c1ad78ba9f4894785cf590d2d68c0b0734f1b Mon Sep 17 00:00:00 2001 From: Cory Fauver Date: Fri, 29 Apr 2016 12:12:17 -0700 Subject: [PATCH 2/4] more detail dropdowns and time trial format explained. --- README.md | 584 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 341 insertions(+), 243 deletions(-) diff --git a/README.md b/README.md index b17f0dca..35b1f759 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,16 @@ | :--- | | Review **CRUD** in the context of a Rails application, especially **update** and **delete**. | | Implement **form helpers** in a Rails application. | +| Get some reps on building a Rails CRUD app. | -Researchers are collecting data on a local bog and need an app to quickly record field data. Your goal is to create a **Bog App**. If you get stuck at any point, feel free to reference the [solution branch](https://github.com/sf-wdi-25/rails_bog_app/tree/solution). +Researchers are collecting data on a local bog and need an app to quickly record field data. Your goal is to create a **Bog App**. If you get stuck at any point, feel free to reference the [solution branch](https://github.com/sf-wdi-27-28/rails_bog_app/tree/solution). + +We want to format this project as a "time trial." You will be building the app 4 times, each time gaining skills through repetition. Here's how we want you to work: + + 1. Start by making a `first-run` branch: `git checkout -b first-run` Go through this entire readme. Use as many hints as you'd like to check your work and make sure you get through the lab smoothly. Commit your work along the way and at the conclusion. + 2. Reset your progress to the beginning by checking out master again `git checkout master` then make a `second-run` branch: `git checkout -b second-run`. Go through the lab another time. This time, time yourself on how long it takes you. Push yourself to peek at the hints more sparingly and code as much as you can on your own. Again, make sure to commit your work. + 3. Reset your progress to the beginning by checking out master again `git checkout master` then make a `third-run` branch: `git checkout -b third-run`. Repeat the lab a third time. Try not to use the instructions to build your bog app and refer to them only when very stuck. Time yourself again and aim to build the app faster than you built it the second time around. + 4. Reset your progress to the beginning by checking out master again `git checkout master` then make a `fourth-run` branch: `git checkout -b fourth-run`. This is the fourth time; streamline your process. Squash bugs faster and look at the resources less. Build it as fast as you can! ## Background @@ -77,7 +85,7 @@ To include the Bootstrap file you just downloaded, require it in `app/assets/sty In Atom, open up `config/routes.rb`. Inside the routes `draw` block, erase all the commented text.
- Hint: It should now look exactly like this... + Throughout the instructions, there will be hints like this one that show you the code. When you're running through the project a second time, try to use these less. The third time, try not to use them at all. Hint: `routes.rb` should now look exactly like this...

```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 -# +

+ Hint: +

+ ```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 + ``` +

+
#### 3. Set up the view for the new creature form @@ -284,86 +297,108 @@ Go to `localhost:3000/creatures/new` in the browser, and inspect the HTML for th Your new creature form has `action="/creatures"` and `method="POST"`. The `POST /creatures` route doesn't exist yet, so go ahead and create it! -```ruby -# -#/config/routes.rb -# +
+ Hint: +

+ + ```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 + ``` +

+
#### 5. Set up the creatures `create` action The `POST /creatures` maps to the `creatures#create` controller action, so the next step is to define the controller method to handle this request. `creatures#create` should add a new creature to the database. -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ The code: +

+ ```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 + ``` +

+
#### 6. Refactor the `new` creature form -Update your `creatures#new` action to send a new instance of a `Creature` to the new creature form: +Update your `creatures#new` action to send a new instance of a `Creature` to the new creature form. -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ Hint: +

+ + ```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 + ``` +

+
This sets `@creature` to a new instance of a `Creature`, which is automatically shared with the form in `views/creatures/new.html.erb`. This allows you to refactor the code for the `form_for` helper. -```html - +
+ It might look something like this: +

+ ```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 %> + ``` +

+
Go to `localhost:3000/creatures/new` again in the browser, and inspect the HTML for the form on the page. Did anything change? @@ -371,91 +406,112 @@ Go to `localhost:3000/creatures/new` again in the browser, and inspect the HTML Right now, your app redirects to `/creatures` after creating a new creature, and the new creature shows up at the bottom of the page. Let's make a route for users to see a specific creature. Then, you'll be able to show a new creature by itself right after it's created. -First, define a `show` route: +First, define a `show` route. -```ruby -# -# config/routes.rb -# +
+ Hint: +

+ ```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 + ``` +

+
-Now that you have your `show` route, set up the controller action for `creatures#show`: +Now that you have your `show` route, set up the controller action for `creatures#show`. -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ Hint: +

-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 + ``` +

+
Next, create the view to display a single creature: -```html - +
+ It might look like this: +

+ ```html + -

<%= @creature.name %>

-

<%= @creature.description %>

-``` +

<%= @creature.name %>

+

<%= @creature.description %>

+ ``` +

+
#### 8. Refactor the `creatures#create` redirect The `creatures#create` method currently redirects to `/creatures`. Again, this isn't very helpful for users who want to verify that they successfully created a *single* creature. The best way to fix this is to have it redirect to `/creatures/:id` instead. -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ Hint: +

+ ```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 + ``` +

+
Make sure to `git add` and `git commit` again once you have `new`, `create`, and `show` working. @@ -468,64 +524,79 @@ Editing a specific creature requires two methods: #### 1. Define a route for the `edit` creature form -```ruby -# -# config/routes.rb -# +
+ Hint: +

+ ```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 + ``` +

+
#### 2. Set up the creatures `edit` action Using your `creatures#new` and `creatures#show` method as inspiration, you can write the `creatures#edit` method in the creatures controller: -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ Hint: +

+ ```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 + ``` +

+
#### 3. Set up the view for the edit creature form Create an `edit.html.erb` view inside `views/creatures`. Jump-start the edit form by copying the form from `views/creatures/new.html.erb` into `views/creatures/edit.html.erb`: -```html - +
+ Hint: +

+ ```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 %> + ``` +

+
Go to `localhost:3000/creatures/1/edit` in the browser to see what it looks like so far. Check the `method` and `action` of the form. Also look at the hidden input with `name="_method"`. What is it doing? The Rails form helper knows to turn this same code into an edit form because you're on the edit page! @@ -533,22 +604,27 @@ Go to `localhost:3000/creatures/1/edit` in the browser to see what it looks like The update route will use the `id` of the creature to be updated. In Express, you decided between `PUT /creatures/:id` and `PATCH /creatures/:id`, depending on the type of update you wanted to do. In Rails, we'll need to add `PATCH /creatures/:id` only to our routes. -```ruby -# -# config/routes.rb -# +
+ Hint: +

+ ```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 + ``` +

+
Run `rake routes` in the Terminal to see the newly created update routes. @@ -556,38 +632,44 @@ Run `rake routes` in the Terminal to see the newly created update routes. In the `CreaturesController`, define an `update` method: -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ Hint: +

-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 + ``` +

+
Test your `creatures#update` method in the browser by editing the creature with an `id` of 1 (go to `localhost:3000/creatures/1/edit`). Then, `git add` and `git commit` your work. @@ -597,23 +679,29 @@ Test your `creatures#update` method in the browser by editing the creature with Following a similar pattern to our other routes, create a route to `destroy` (delete) a specific creature based on its `id`. -```ruby -# -# config/routes.rb -# +
+ Hint: +

-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 + ``` +

+
At this point, you're using all the RESTful routes for creatures. @@ -622,39 +710,47 @@ At this point, you're using all the RESTful routes for creatures. In the `CreaturesController`, define an `destroy` method: -```ruby -# -# app/controllers/creatures_controller.rb -# +
+ Hint: +

+ ```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 + ``` +

+
#### 3. Add a delete button Add a delete button to the view that displays a single creature: +
+ It could look something like: +

```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 %> ``` +

+
Visit `localhost:3000/creatures/1` in the browser, and inspect the HTML for the delete button. Click the delete button to manually test this feature. @@ -671,8 +769,8 @@ At this point, you've created all the RESTful routes, implemented controller act ## Bonus * Add a Bootstrap `navbar` with links to the homepage (`/`) and the new creatures page (`/creatures/new`). Also link each creature on `creatures#index` to its individual `show` page. -* Read about Active Record Validations, and add validations to the `Creature` model to make sure a new creature can't be created without a `name` and `description`. -* Read the docs for the Paperclip gem, and incorporate it into your Bog App to upload photos of creatures. +* Read about [Active Record Validations](http://guides.rubyonrails.org/active_record_validations.html), and add validations to the `Creature` model to make sure a new creature can't be created without a `name` and `description`. +* Read the docs for the [Paperclip gem](https://github.com/thoughtbot/paperclip), and incorporate it into your Bog App to upload photos of creatures. ## Submission From ffd84d338ff4e31875ddfa3587eb52fc63aa1baa Mon Sep 17 00:00:00 2001 From: Cory Fauver Date: Fri, 29 Apr 2016 12:17:33 -0700 Subject: [PATCH 3/4] some small typos --- README.md | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 35b1f759..ad9905ec 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ Researchers are collecting data on a local bog and need an app to quickly record We want to format this project as a "time trial." You will be building the app 4 times, each time gaining skills through repetition. Here's how we want you to work: - 1. Start by making a `first-run` branch: `git checkout -b first-run` Go through this entire readme. Use as many hints as you'd like to check your work and make sure you get through the lab smoothly. Commit your work along the way and at the conclusion. + 1. Start by making a `first-run` branch: `git checkout -b first-run`. Move through the instructions below to build your bog app. Use as many hints as you'd like to check your work and make sure you get through the lab smoothly. Commit your work along the way and at the conclusion. 2. Reset your progress to the beginning by checking out master again `git checkout master` then make a `second-run` branch: `git checkout -b second-run`. Go through the lab another time. This time, time yourself on how long it takes you. Push yourself to peek at the hints more sparingly and code as much as you can on your own. Again, make sure to commit your work. - 3. Reset your progress to the beginning by checking out master again `git checkout master` then make a `third-run` branch: `git checkout -b third-run`. Repeat the lab a third time. Try not to use the instructions to build your bog app and refer to them only when very stuck. Time yourself again and aim to build the app faster than you built it the second time around. - 4. Reset your progress to the beginning by checking out master again `git checkout master` then make a `fourth-run` branch: `git checkout -b fourth-run`. This is the fourth time; streamline your process. Squash bugs faster and look at the resources less. Build it as fast as you can! + 3. Reset your progress to the beginning by checking out master again `git checkout master` then make a `third-run` branch: `git checkout -b third-run`. Repeat the lab a third time. Try not to use the instructions to build your bog app and refer to them only when very stuck. Time yourself again and aim to build the app faster than you built it the second time around. Make sure you have roughly the same number of commits as you had on your second run. Version control isn't the place to cut corners! + 4. Reset your progress to the beginning by checking out master again `git checkout master` then make a `fourth-run` branch: `git checkout -b fourth-run`. This is the fourth time; streamline your process. Squash bugs faster and look at the resources less. Commit often and build it as fast as you can! ## Background @@ -325,33 +325,33 @@ The `POST /creatures` maps to the `creatures#create` controller action, so the n
The code:

- ```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 + ```

From 7da76af7cb5ba63742c598442fcdd92118dba089 Mon Sep 17 00:00:00 2001 From: Nick Brennan Date: Wed, 4 May 2016 20:20:48 -0700 Subject: [PATCH 4/4] missing -d before postgresql in Part I --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ad9905ec..7c19b948 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ REST stands for **REpresentational State Transfer**. We will strictly adhere to Fork this repo, and clone it into your `wdi` folder on your local machine. Change directories into `rails-bog-app`, and create a new Rails project: ```zsh -➜ rails new bog_app -T postgresql +➜ rails new bog_app -T -d postgresql ➜ rake db:create ➜ rails s ```