- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 Rails 5.1 API 应用程序中有如下所示的 Dish
和 Comment
模型 - 代码库 here .我需要帮助将新的 Comment
添加到 Dish
。
发布
class Dish < ApplicationRecord
has_many :comments
end
评论
class Comment < ApplicationRecord
belongs_to :dish
end
后序列化器(使用 ActiveModel Seriazlier)
class DishSerializer < ActiveModel::Serializer
attributes :id, :name, :image, :category, :label, :price, :featured, :description, :created_at
has_many :comments
end
注释序列化器
class CommentSerializer < ActiveModel::Serializer
attributes :id, :rating, :comment, :author, :date
def date
object.created_at
end
end
Post Controller - 默认 rails 脚手架
class DishesController < ApplicationController
before_action :set_dish, only: [:show, :update, :destroy]
# GET /dishes
def index
@dishes = Dish.all
render json: @dishes
end
# GET /dishes/1
def show
render json: @dish
end
# POST /dishes
def create
@dish = Dish.new(dish_params)
if @dish.save
render json: @dish, status: :created, location: @dish
else
render json: @dish.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /dishes/1
def update
# byebug
if @dish.update(dish_params)
render json: @dish
else
render json: @dish.errors, status: :unprocessable_entity
end
end
# DELETE /dishes/1
def destroy
@dish.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_dish
@dish = Dish.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def dish_params
params.require(:dish).permit(:name, :image, :category, :label, :price, :featured, :description)
end
end
评论 Controller - 默认 rails 脚手架
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :update, :destroy]
# GET /comments
def index
@comments = Comment.all
render json: @comments
end
# GET /comments/1
def show
render json: @comment
end
# POST /comments
def create
@comment = Comment.new(comment_params)
if @comment.save
render json: @comment, status: :created, location: @comment
else
render json: @comment.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /comments/1
def update
if @comment.update(comment_params)
render json: @comment
else
render json: @comment.errors, status: :unprocessable_entity
end
end
# DELETE /comments/1
def destroy
@comment.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def comment_params
params.require(:comment).permit(:rating, :comment, :author)
end
end
问题
当用户访问 /dishes/:id
并通过前端应用程序(Angular 2)向菜品添加评论时,评论
被推送到当前评论的数组,我正在调用 PUT/dishes:id
,dish
对象嵌套在现有的 comments
和新评论中。然而,新的 comment
没有被 rails 保存——没有返回错误,而是返回了 dish
对象。但是我确实在 rails s
控制台中看到了 Unpermitted parameters: :id, :created_at
。如何让 Rails 保存新评论?
在 Angular 客户端,我向菜品添加评论的页面 ( dishes/9
) 如下所示。
Rails 服务器日志
在 Rails 方面,下面是我在 params
中看到的 - 我确实看到了新评论 - {"author"=>"JANE7777", "rating"=>3, "comment"=>"COMMENT7777", "date"=>"2017-11-12T12:58:12.555Z"}
在那里。
Started PUT "/dishes/9" for 127.0.0.1 at 2017-11-12 18:28:12 +0530
Processing by DishesController#update as HTML
Parameters: {"id"=>"9", "name"=>"Uthappizza", "image"=>"images/uthappizza.png", "category"=>"mains", "label"=>"Hot", "price"=>"4.99", "featured"=>true, "description"=>"A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.", "created_at"=>"2017-11-01T04:30:09.407Z", "comments"=>[{"id"=>46, "rating"=>5, "comment"=>"Imagine all the eatables, living in conFusion!", "author"=>"John Lemon", "date"=>"2012-10-16T17:57:28.556Z"}, {"id"=>47, "rating"=>4, "comment"=>"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", "author"=>"Paul McVites", "date"=>"2014-09-05T17:57:28.556Z"}, {"id"=>48, "rating"=>3, "comment"=>"Eat it, just eat it!", "author"=>"Michael Jaikishan", "date"=>"2015-02-13T17:57:28.556Z"}, {"id"=>49, "rating"=>4, "comment"=>"Ultimate, Reaching for the stars!", "author"=>"Ringo Starry", "date"=>"2013-12-02T17:57:28.556Z"}, {"id"=>50, "rating"=>2, "comment"=>"It's your birthday, we're gonna party!", "author"=>"25 Cent", "date"=>"2011-12-02T17:57:28.556Z"}, {"id"=>51, "rating"=>4, "comment"=>"great dish", "author"=>"Jogesh", "date"=>"2017-10-30T05:03:39.656Z"}, {"author"=>"JANE7777", "rating"=>3, "comment"=>"COMMENT7777", "date"=>"2017-11-12T12:58:12.555Z"}], "dish"=>{"id"=>"9", "name"=>"Uthappizza", "image"=>"images/uthappizza.png", "category"=>"mains", "label"=>"Hot", "price"=>"4.99", "featured"=>true, "description"=>"A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.", "created_at"=>"2017-11-01T04:30:09.407Z"}}
Dish Load (1.0ms) SELECT "dishes".* FROM "dishes" WHERE "dishes"."id" = $1 LIMIT $2 [["id", 9], ["LIMIT", 1]]
[25, 34] in C:/apps/railsApi/app/controllers/dishes_controller.rb
25: end
26:
27: # PATCH/PUT /dishes/1
28: def update
29: byebug
=> 30: if @dish.update(dish_params)
31: render json: @dish
32: else
33: render json: @dish.errors, status: :unprocessable_entity
34: end
(byebug) params
<ActionController::Parameters {"id"=>"9", "name"=>"Uthappizza", "image"=>"images/uthappizza.png", "category"=>"mains", "label"=>"Hot", "price"=>"4.99", "featured"=>true, "description"=>"A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.", "created_at"=>"2017-11-01T04:30:09.407Z", "comments"=>[{"id"=>46, "rating"=>5, "comment"=>"Imagine all the eatables, living in conFusion!", "author"=>"John Lemon", "date"=>"2012-10-16T17:57:28.556Z"}, {"id"=>47, "rating"=>4, "comment"=>"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", "author"=>"Paul McVites", "date"=>"2014-09-05T17:57:28.556Z"}, {"id"=>48, "rating"=>3, "comment"=>"Eat it, just eat it!", "author"=>"Michael Jaikishan", "date"=>"2015-02-13T17:57:28.556Z"}, {"id"=>49, "rating"=>4, "comment"=>"Ultimate, Reaching for the stars!", "author"=>"Ringo Starry", "date"=>"2013-12-02T17:57:28.556Z"}, {"id"=>50, "rating"=>2, "comment"=>"It's your birthday, we're gonna party!", "author"=>"25 Cent", "date"=>"2011-12-02T17:57:28.556Z"}, {"id"=>51, "rating"=>4, "comment"=>"great dish", "author"=>"Jogesh", "date"=>"2017-10-30T05:03:39.656Z"}, {"author"=>"JANE7777", "rating"=>3, "comment"=>"COMMENT7777", "date"=>"2017-11-12T12:58:12.555Z"}], "controller"=>"dishes", "action"=>"update", "dish"=>{"id"=>9, "name"=>"Uthappizza", "image"=>"images/uthappizza.png", "category"=>"mains", "label"=>"Hot", "price"=>"4.99", "featured"=>true, "description"=>"A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.", "created_at"=>"2017-11-01T04:30:09.407Z"}} permitted: false>
(byebug) c
Unpermitted parameters: :id, :created_at
(0.0ms) BEGIN
(0.0ms) COMMIT
[active_model_serializers] Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."dish_id" = $1 [["dish_id", 9]]
[active_model_serializers] Rendered DishSerializer with ActiveModelSerializers::Adapter::Attributes (31.29ms)
Completed 200 OK in 1901725ms (Views: 37.5ms | ActiveRecord: 5.0ms)
客户端模型
Dish
模型的成员之一是 Comment[]
。当通过表单添加新评论时,comment
被推送到 dish.comments
数组,然后再将 Dish
对象发送到 Rails API back-结束。
评论
模型
export class Comment {
rating: number;
comment: string;
author: string;
date: string;
}
在客户端发布
模型
import { Comment } from './comment';
export class Dish {
id: number;
name: string;
image: string;
category: string;
label: string;
price: string;
featured: boolean;
description: string;
comments: Comment[];
}
最佳答案
我建议您使用 Comments Controller 并形成一个嵌套路由以实现 RESTful。
首先,POST 请求,包括 dish_id 到/comments。您正在尝试创建评论,而不是更新菜肴。
其次,形成嵌套路由:/菜品/:dish_id/comments
这是嵌套资源的指南:http://guides.rubyonrails.org/routing.html#nested-resources
您出错的原因是您破坏了预期的参数结构。对于 PUT,您的参数应该是:
{
id: <record to update>,
dish: {
name: “blah”,
comments: [{...},{...}]
}
}
再次强调,如果您只想添加评论,我强烈建议您不要 PUT 到 Dish。例如:上面的内容需要替换所有关于 Dish 的评论!如果您要创建记录,请创建该记录,不要更新它的父级。
如果您想在添加新评论时更新 Dish,您可以在创建时向评论模型添加回调,或在评论创建 Controller 操作中执行此操作。
关于ruby-on-rails - PUT 嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47249461/
以下是一个非常简单的ruby服务器。 require 'socket' local_socket = Socket.new(:INET, :STREAM) local_addr = Socket.
我正在使用 OS X(使用 bash),并且是 unix 的新手。我想知道是否可以修改一些文件以便运行 ruby 程序,我不需要“ruby file.rb”,而是可以运行“ruby.rb”。 有理
我在用 Ruby 替换字符串时遇到一些问题。 我的原文:人之所为不如兽之所为。 我想替换为:==What== human does is not like ==what== animal does.
我想在一个循环中从 Ruby 脚本做这样的事情: 写一个文件a.rb(每次迭代都会改变) 执行系统(ruby 'a.rb') a.rb 将带有结果的字符串写入文件“results” a.rb 完成并且
我的问题是尝试创建一个本地服务器,以便我可以理解由我的新团队开发的应用程序。我的问题是我使用的是 Ruby 2.3.3,而 Gemfile 需要 2.3.1。我无法编辑 Gemfile,因为我被告知很
我有一个使用 GLI 框架用 Ruby 编写的命令行实用程序。我想在我的主目录中配置我的命令行实用程序,使用 Ruby 本身作为 DSL 来处理它(类似于 Gemfile 或 Rakefile)。 我
我的 Rails 应用 Controller 中有这段代码: def delete object = model.datamapper_class.first(:sourced_id =>
我正在寻找的解析器应该: 对 Ruby 解析友好, 规则设计优雅, 产生用户友好的解析错误, 用户文档的数量应该比计算器示例多, UPD:允许在编写语法时省略可选的空格。 快速解析不是一个重要的特性。
我刚开始使用 Ruby,听说有一种“Ruby 方式”编码。除了 Ruby on Rails 之外,还有哪些项目适合学习并被认可且设计良好? 最佳答案 Prawn被明确地创建为不仅是一个该死的好 PDF
我知道之前有人问过类似的问题,但是我该如何构建一个无需在前面输入“ruby”就可以在终端中运行的 Ruby 文件呢? 这里的最终目标是创建一个命令行工具包类型的东西。现在,为了执行我希望用户能够执行的
例如哈希a是{:name=>'mike',:age=>27,:gender=>'male'}哈希 b 是 {:name=>'mike'} 我想知道是否有更好的方法来判断 b 哈希是否在 a 哈希内,而
我是一名决定学习 Ruby 和 Ruby on Rails 的 ASP.NET MVC 开发人员。我已经有所了解并在 RoR 上创建了一个网站。在 ASP.NET MVC 上开发,我一直使用三层架构:
最近我看到 Gary Bernhardt 展示了他用来在 vim 中执行 Ruby 代码的 vim 快捷方式。捷径是 :map ,t :w\|:!ruby %. 似乎这个方法总是执行系统 Rub
在为 this question about Blue Ruby 选择的答案中,查克说: All of the current Ruby implementations are compiled to
我有一个 Ruby 数组 > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters"
我在四舍五入时遇到问题。我有一个 float ,我想将其四舍五入到小数点后的百分之一。但是,我只能使用 .round ,它基本上将它变成一个 int,意思是 2.34.round # => 2. 有没
我使用 ruby on rails 编写了一个小型 Web 应用程序,它的主要目的是上传、存储和显示来自 xml(文件最多几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程
我们如何用 Ruby 转换像这样的字符串: 𝑙𝑎𝑡𝑜𝑟𝑟𝑒 收件人: Latorre 最佳答案 s = "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" => "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" s.u
通过 ruby monk 时,他们偶尔会从左侧字段中抛出一段语法不熟悉的代码: def compute(xyz) return nil unless xyz xyz.map {|a,
不确定我做错了什么,但我似乎弄错了。 问题是,给你一串空格分隔的数字,你必须返回最大和最小的数字。 注意:所有数字都是有效的 Int32,不需要验证它们。输入字符串中始终至少有一个数字。输出字符串必须
我是一名优秀的程序员,十分优秀!