【发布时间】:2018-01-28 09:58:44
【问题描述】:
我是 Ruby on Rails 和 Web 开发的初学者。我正在参加在线课程,但讲座示例一直失败。
我的问题是我的课程控制器,索引操作。
控制器:my_first_app/controllers/courses_controller.rb
class CoursesController < ApplicationController
def index
@search_term = 'jhu'
@courses = Coursera.for(@search_term)
end
end
型号: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
查看:my_first_app/views/index.html.erb
<h1>Search for - <%= @search_term %> </h1>
<table border = "1">
<tr>
<th>Image</th>
<th>Name</th>
<th>Description</th>
</tr>
<%= @courses.each do |course| %>
<tr>
<td><%= image_tag(course["smallIcon"]) %></td>
<td><%= course["name"] %></td>
<td><%= course["shortDescription"] %> </td>
</tr>
<% end %>
路由:my_first_app/config/routes.rb
Rails.application.routes.draw do
get 'courses/index'
get 'greeter/hello'
get 'greeter/goodbye'
end
宝石文件:
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 do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'httparty', '0.13.5'
end
我将最后一个 gem 添加到 Gemfile 并运行包,然后我控制 +c 停止并重新启动服务器,我收到错误消息:
JSON::ParserError in CoursesController#index
我认为这是说我解析的是 HTML 而不是 JSON,但我不知道如何修复它。任何帮助将不胜感激。 原始仓库:https://github.com/jhu-ep-coursera/fullstack-course1-module3
【问题讨论】:
标签: html css ruby-on-rails json ruby