ruby-on-rails - 我的 Ruby on Rails 应用程序页面只打印 JSON 数据,不读取任何 HTML
问题描述
我正在尝试使用 Foundation-rails 创建一个不错的网站,但由于某种原因,没有读取 application.html.erb,并且只显示了从 .csv 文件生成的 JSON 数据。
我已经尝试根据我在网上找到的内容更改很多变量,但现在很难知道什么是正确的,因为很多基础导轨指南现在已经有 5 年的历史了。无论文件的内容是什么,我似乎根本无法渲染 application.html.erb。
[{"id":1,"name":"Large Speaker","price":199.99,"inventory_count":3,"description":"Very large. Loud? Prone to breaking.","created_at":"2019-01-20T05:31:53.080Z","updated_at":"2019-01-20T05:31:53.080Z"},{"id":2,"name":"Small Speaker","price":299.99,"inventory_count":34,"description":"Very small. Ineffective, but stylish.","created_at":"2019-01-20T05:31:53.083Z","updated_at":"2019-01-20T05:31:53.083Z"},{"id":3,"name":"Wrench","price":29.99,"inventory_count":1,"description":"Good for fixing speakers.","created_at":"2019-01-20T05:31:53.086Z","updated_at":"2019-01-20T05:31:53.086Z"},{"id":4,"name":"Golden Plate","price":1.99,"inventory_count":104,"description":"It’s just paint. Probably toxic. Great for weddings.","created_at":"2019-01-20T05:31:53.088Z","updated_at":"2019-01-20T05:31:53.088Z"}]
这是 localhost:3000/products 上显示的内容。应该显示的是带有文本框和按钮的标准 HTML 页面。我的控制台或 Chrome 的控制台中没有出现错误。
考虑到我还是 Rails 新手,我也不完全确定应该分享哪些代码片段或文件。请让我知道您是否想仔细查看任何内容。
编辑:
应用程序.html.erb
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<!--Various elements-->
<%= yield %>
<%= javascript_include_tag "application" %>
</body>
</html>
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :update, :destroy]
# GET /products
def index
@products = Product.all
render "application.html.erb", json: @products
end
# GET /products/1
def show
render json: @product
end
# POST /products
def create
@product = Product.new(product_params)
if @product.save
render json: @product, status: :created, location: @product
else
render json: @product.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /products/1
def update
if @product.update(product_params)
render json: @product
else
render json: @product.errors, status: :unprocessable_entity
end
end
# DELETE /products/1
def destroy
@product.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def product_params
params.require(:product).permit(:name, :price, :inventory_count, :description)
end
end
解决方案
推荐阅读
- python - 熊猫 sort_index 文档
- python - 将系列转换为字典?
- signalr - 用于 WebSocket 连接的 ProxyKit 和 Querystring
- javascript - 保留 if 语句中对变量的更改
- javascript - 如何根据月份获取 dateRange
- javascript - 使用 API 插入公共 Google 日历
- python - 如何计算具有 str 类型的列中的行相对于它们的类别的出现次数?
- arduino - 如何与 HM-19 BLE 模块通信并使用超声波传感器进行扫描
- python - 字典列表列表
- angular - API返回后,ionic 4 LoadingController不会关闭