html - JSON::ParserError in CoursesController#index -
i beginner of ruby on rails , web development. taking online course , keep failing lecture example.
my problem courses_controller, index action.
controller: my_first_app/controllers/courses_controller.rb
class coursescontroller < applicationcontroller def index @search_term = 'jhu' @courses = coursera.for(@search_term) end end
model: my_first_app/models/coursera.rb
class coursera include httparty #default_options.update(verify: false) # turn off ssl verification base_uri 'https://api.coursera.org/api/catalog.v1/courses' default_params fields: "smallicon,shortdescription", q: "search" format :json def self.for term get("", query: { query: term})["elements"] end end
view: my_first_app/views/index.html.erb
<h1>search - <%= @search_term %> </h1> <table border = "1"> <tr> <th>image</th> <th>name</th> <th>description</th> </tr> <%= @courses.each |course| %> <tr> <td><%= image_tag(course["smallicon"]) %></td> <td><%= course["name"] %></td> <td><%= course["shortdescription"] %> </td> </tr> <% end %>
routes: my_first_app/config/routes.rb
rails.application.routes.draw 'courses/index' 'greeter/hello' 'greeter/goodbye' end
gemfile:
source 'https://rubygems.org' gem 'rails', '4.2.3' gem 'sqlite3' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc group :development, :test gem 'byebug' gem 'web-console', '~> 2.0' gem 'spring' gem 'httparty', '0.13.5' end
i added last gem gemfile , run bundle, controlled +c stopped , restarted server , got error message:
json::parsererror in coursescontroller#index
i think saying parse html instead of json, don't know how fix it. appreciated. original repository: https://github.com/jhu-ep-coursera/fullstack-course1-module3
according api documentation coursera, looks base_uri
might need be:
base_uri 'https://api.coursera.org/api/courses.v1'
and fields
in default_params
should (maybe) be:
default_params fields: "photourl,description", q: "search"
(changing other occurrences of 'smallicon'
, 'shortdescription'
'photourl'
, 'description'
in app well, since don't see fields in api docs).
once changes made should end going urls https://api.coursera.org/api/courses.v1?q=search&query=ruby&fields=photourl,description returning valid json. current base_uri
using sending html error page telling action not found. since that's html page, json can't parse it.
if @ date on last commit in github repository linked, last commit in november 2015, 2 years ago. super out of date, api has changed fair bit since then.
Comments
Post a Comment