GithubHelp home page GithubHelp logo

cookies_and_sessions_lab's Introduction

Cookies and Sessions Lab

Objective

We're going to make a very simple shopping cart, stored entirely in the session.

Introduction

The Rails session method gives us access to the Rails session. The session is a datastore implemented with cookies. You can store simple data structures in the session. ActiveRecord models, no. Arrays of strings or numbers, yes. Basically, stick to data literals—numbers, strings, hashes, and arrays.

We're going to use this to implement a shopping site. Here's how the site will work:

  1. The root page of the app has an input box on it (within a form of course).
  2. The user types in the item they want and clicks add to cart.
  3. The item is added to their cart. The page shows everything in the user's cart.

Instructions

  1. Create the cart method in ApplicationController#cart, this method should return an array of the items stored in the cart (utilizing the Rails session method).
  2. Create a Products controller with two actions, index and add.
  3. Create the routes for the application, we only need two routes, one to display the Products#index (the root route) and one to post the products to add them to the cart.
  4. Create views using the feature tests as your guide. The page should have, at a minimum: a text box where the user can enter the name of a product, a submit button that adds it to their cart, and a display of what's in the cart.

For this lab, there is no need to create tables and models and store the products in the database. We'll be using cookies via the Rails session method, along with our cart helper method to persist the products to the cart and to display them in the view.

cookies_and_sessions_lab's People

Contributors

ahimmelstoss avatar annjohn avatar blake41 avatar bperl avatar danielseehausen avatar gj avatar ihollander avatar johann avatar lukeghenco avatar maxwellbenton avatar mendelb avatar mmacdonald1 avatar pletcher avatar queerviolet avatar sgharms avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cookies_and_sessions_lab's Issues

Using method described in readme will not pass lab

The Readme for this lab describes adding the current_cart method as a helper_method in the ApplicationController

This way, we can use current_cart in our views and layouts too. For example, we may want to show the user how many items are in their cart as part of the layout.

However the spec/views/products_index_spec.rb seems to be testing for a instance variable @cart to be the method of displaying the items in the view. In fact due to a weird issue of helper methods not being included in Rspec view specs, using the prescribed method in the Readme would not work and results in an error to the tune of:

undefined method `cart' for #<ActionView::TestCase::TestController:0x007fbbb28a8778>

Perhaps we should ease the test a little to be more flexible. Or due to the Rspec problem - advise in this lab to use an instance variable.

Issue with Gem Bundler

Hello I received this error when trying to run the lab:
`Bundler could not find compatible versions for gem "bundler":
In Gemfile:
bundler (~> 1.3, >= 1.3.1)

Current Bundler version:
bundler (2.0.1)
This Gemfile requires a different version of Bundler.
Perhaps you need to update Bundler by running gem install bundler?

Could not find gem 'bundler (~> 1.3, >= 1.3.1)' in any of the relevant sources:
the local ruby installation`

Lab instructions unclear. Solution a bit advanced.

The instructions are a bit vague as to what the setup should be to pull things together. The solution also seems to be using logic far different then what is explained in the Readme for this Lab.
I've seen many pull requests for the v-000 version seem to be copied or obviously influenced by/from the solution

Breaks if you don't name your variable 'cart'

require 'rails_helper'

RSpec.describe "products/index", :type => :view do
it "shows everything in the cart" do
assign(:cart, ['apples', 'bananas', 'pears'])
render
expect(rendered).to include 'apples'
expect(rendered).to include 'bananas'
expect(rendered).to include 'pears'
end

if you name your variable anything other than 'cart' it won't work. very annoying unless you read the spec closely

Running learn throws Thor error

Running learn command results in error:

/usr/local/rvm/gems/ruby-2.3.1/gems/thor-0.20.1/lib/thor/error.rb:12:in `module:DidYouMean': uninitialized constant Thor::DidYouMean::SpellChecker (NameError)

Not sure how to resolve the issue. Tried reinstalling gem, adding it to gem file etc...

Cart.rb test raises capybara errors

First tried to run the "feature/cart.rb" test, and had to rename the folder and file to "features/cart_spec.rb" so that the learn gem could find it properly.

Running the test yielded this capybara error:

NoMethodError: undefined methodvisit' for #RSpec::ExampleGroups::Homepage:0x007f9882f21008`

I added
require 'capybara/rails' require 'capybara/rspec'

to the rails_helper, which did not change the error. Some Stack Exchange answers suggested changing the type in the describe block to feature (previously :view), so I changed it to this line:

RSpec.describe "homepage", :type => :feature do

Now the test progressed further! And raised this new error:

`Failure/Error: fill_in 'product', 'kumquats'

 RuntimeError:
   Must pass a hash containing 'with'`

So I changed the applicable line to fill_in 'product', with: 'kumquats'
Which produced a new error:
`Failure/Error: expect(page).to include 'kumquats'

   expected #<Capybara::Session> to include "kumquats", but it does not respond to `include?`
   Diff:
   @@ -1,2 +1,2 @@
   -["kumquats"]
   +#<Capybara::Session>`

Still trying to figure out how to get past this one.

products_index_spec seems wacky

The spec is:
it "shows everything in the cart" do session[:cart] = ['apples', 'bananas', 'pears'] render expect(rendered).to include 'apples' expect(rendered).to include 'bananas' expect(rendered).to include 'pears' end

the #render method renders the '/' template without first going to controller for '/'. So, the only way to get this test to pass seems to be to put logic into the index view that would normally go in a controller. This test also seems to contradict the expectatiosn of the cart spec:
it "adds items to the cart when they're submitted via its form" do visit '/' fill_in 'product', with: 'kumquats' click_button 'add to cart' expect(page.body).to include 'kumquats' end

which makes it so that you have to put logic in the controller to get it to pass. I got both tests to pass, but I had to put in some really jancky conditional logic into the view telling the view to either use an array that is sent from the controller or, if that array doesn't exist, assign the array directly to session[:cart].

Maybe there's a simpler way to get both to pass, but it's not very apparent if there is.

Might need to add web-console to gemfile

Hi. In case helpful:

  • After cloning the lesson and while working in my own environment, I kept getting the following error whenever I ran rspec out of the gate:

NoMethodError: undefined method 'web_console' for #<Rails::Application::Configuration:0x007fe5511984b0> Did you mean? console

(The whole error is reproduced below. My ruby version is: 2.3.1p112. I did not try any of this out in the learn ide.)

  • Running bundle update and/or bundle exec rspec did not help. What solved the issue was adding this to the gemfile:

gem 'web-console', '~> 2.0'

That cleared everything up.

Thanks!


Here's the whole error:

NoMethodError:
  undefined method `web_console' for #<Rails::Application::Configuration:0x007fe5511984b0>
  Did you mean?  console
# /Users/adistinti/.rvm/gems/ruby-2.3.1/gems/railties-4.2.10/lib/rails/railtie/configuration.rb:95:in `method_missing'
# ./config/application.rb:11:in `<class:Application>'
# ./config/application.rb:10:in `<module:CookiesAndSessionsLab>'
# ./config/application.rb:9:in `<top (required)>'
# ./config/environment.rb:2:in `require'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `require'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/controllers/application_controller_spec.rb:1:in `require'
# ./spec/controllers/application_controller_spec.rb:1:in `<top (required)>'

Typo in Products/index test

Please replace session[:cart] = ['apples', 'bananas', 'pears']
with assign(:cart, ['apples', 'bananas', 'pears'])

Index view is tested without utilizing controller

The passing test for this lab requires that you access your session from inside of the view file without the use of a carrying instance variable, negating the process of writing a helper method to access and update the session. The problem is that the test itself (in "products_index_spec") simply sets the session and renders the view without ever utilizing the controller. Additionally, the test in the "products_index_spec" file is redundant, because the "cart_spec" has already tested for whether the item is rendered to the page upon submission.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.