use*_*440 7 ruby-on-rails angular
我Dish和Comment模型,如低于我的Rails 5.1 API的应用程序-代码回购这里.我需要帮助添加一个新Comment的Dish.
岗位
class Dish < ApplicationRecord
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
评论
class Comment < ApplicationRecord
belongs_to :dish
end
Run Code Online (Sandbox Code Playgroud)
Post Serializer(使用ActiveModel Seriazlier)
class DishSerializer < ActiveModel::Serializer
attributes :id, :name, :image, :category, :label, :price, :featured, :description, :created_at
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
评论序列化器
class CommentSerializer < ActiveModel::Serializer
attributes :id, :rating, :comment, :author, :date
def date
object.created_at
end
end
Run Code Online (Sandbox Code Playgroud)
后控制器 - 默认导轨脚手架
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
Run Code Online (Sandbox Code Playgroud)
评论控制器 - 默认导轨脚手架
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
Run Code Online (Sandbox Code Playgroud)
问题
当用户访问/dishes/:id并通过前端应用程序(Angular 2)向菜肴添加注释时,comment将推送到当前注释的数组,并且我正在PUT /dishes:id使用dish嵌套了现有comments注释和新注释的对象进行调用.但是新comment的不是由rails保存的 - 没有返回错误,而是返回了dish对象.但是我确实Unpermitted parameters: :id, :created_at在rails s控制台中看到了.如何获取rails来保存新评论?
dishes/9我将评论添加到菜肴的页面()在Angular客户端看起来如下所示.

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)
Run Code Online (Sandbox Code Playgroud)
客户端模型
该Dish模型Comment[]作为其中一个成员.当通过表单添加新注释时comment,dish.comments在将Dish对象发送到Rails API后端之前推送到阵列.
Comment 客户端的模型
export class Comment {
rating: number;
comment: string;
author: string;
date: string;
}
Run Code Online (Sandbox Code Playgroud)
Post 客户端的模型
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[];
}
Run Code Online (Sandbox Code Playgroud)
I\xe2\x80\x99d 建议您使用 Comments 控制器并形成一个嵌套路由以实现 RESTful。
\n\n首先,将请求(包括dish_id)发布到/comments。您\xe2\x80\x99正在尝试创建评论,而不是更新菜肴。
\n\n其次,形成嵌套路由:\n/dishes/:dish_id/comments
\n\nHere\xe2\x80\x99s 是这样的嵌套资源指南:http://guides.rubyonrails.org/routing.html#nested-resources
\n\n错误的原因是您\xe2\x80\x99已经破坏了预期的参数结构。对于 PUT 你的参数应该是:
\n\n{\n id: <record to update>,\n dish: {\n name: \xe2\x80\x9cblah\xe2\x80\x9d,\n comments: [{...},{...}]\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n再次,如果您只想添加评论,我强烈建议您不要将\xe2\x80\x99t PUT to Dish。例如:上面的内容需要替换该菜品上的所有评论!如果您\xe2\x80\x99 正在创建一条记录,请创建该记录,不要\xe2\x80\x99 更新它\xe2\x80\x99 的父记录。
\n\n如果您想在添加新评论时更新 Dish,您可以在创建时向评论模型添加回调,或者在评论创建控制器操作中执行此操作。
\n| 归档时间: |
|
| 查看次数: |
382 次 |
| 最近记录: |