Giter VIP home page Giter VIP logo

using-to-json-lab-v-000's Introduction

Using To Json Lab

Objectives

  1. Explain how to use to_json to display data.
  2. Use respond_to to render JSON or HTML from the same action.

Introduction

We're going to convert the previous lab, where we created our own ProductSerializer, to use to_json instead. The solution for the previous lab has been included.

Outline

  1. Replace using ProductSerializer with using to_json to serialize the Product. Only include the product id, name, description, inventory and price in the JSON response.
  2. Remove the old products/:id/data route and action and set products#show to render JSON or HTML depending on the format of the request.
  3. Modify the show.html.erb page code to properly request JSON from the products/id route.
  4. Make sure tests pass! Some will pass at the beginning. The trick is making sure they all still pass at the end!

Note: If you do everything correctly, you won't have to change any of the code that constructs the show view, only the route to request the data.

View Using to_json Lab on Learn.co and start learning to code for free.

using-to-json-lab-v-000's People

Watchers

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

using-to-json-lab-v-000's Issues

Needed to remove rails version from gemfile for tests to pass

I kept getting these 4 errors:
`Failures:

  1. ProductsController POST create creates a new product
    Failure/Error: post :create, product: @product_attributes

    ThreadError:
    already initialized

    ./spec/controllers/products_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

  2. ProductsController GET inventory returns true or false appropriately
    Failure/Error: get :inventory, id: p1.id

    ThreadError:
    already initialized

    ./spec/controllers/products_controller_spec.rb:23:in `block (3 levels) in <top (required)>'

  3. ProductsController GET description returns the description as a plain string
    Failure/Error: get :description, id: product.id

    ThreadError:
    already initialized

    ./spec/controllers/products_controller_spec.rb:33:in `block (3 levels) in <top (required)>'

  4. ProductsController GET show returns a JSON representation of the product
    Failure/Error: get :show, id: product.id, format: 'json'

    ThreadError:
    already initialized

    ./spec/controllers/products_controller_spec.rb:41:in `block (3 levels) in <top (required)>'

Finished in 6.08 seconds (files took 2.67 seconds to load)
15 examples, 4 failures`

After removing the version '~> 4.2' from rails in the gemfile, deleting gemfile.lock, and running bundle, errors were gone and all tests passed. I hope this helps someone who has the same issue!

Cliver::Dependency::NotFound:

Hello -- I am working on this lab and the earlier labs with Ubuntu, and keep reaching the error --

1.2) Failure/Error:
        raise Dependency::NotFound.new(
          "Could not find an executable #{@executables} on your path.")

      Cliver::Dependency::NotFound:
        Could not find an executable ["phantomjs"] on your path.

I believe there is a chance that this gem does not work on Ubuntu.
(colszowka/phantomjs-gem#99)

Problem with Faker Number Generator

Error: 1) invoices_index_view renders the invoice partial
Failure/Error: Product.create(:name => products.sample, :price => Faker::Number.between(1,1500))
ArgumentError:
wrong number of arguments (given 2, expected 0)

Previous:
Product.create(:name => products.sample, :price => Faker::Number.between(1,1500))

Fix:
Product.create(:name => products.sample, :price => Faker::Number.between(from: 1,to: 1500))

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.