ruby-on-rails - ActionController::UnknownFormat (ActionController::UnknownFormat)
问题描述
好吧,我正在尝试从数据库中更新一些寄存器,但它一直在提高 unknownFormat 错误,你们知道我错过了什么吗?
它有点基本,但我觉得我在这里错过了一些非常重要的东西
@contributor = Contributor.find(params[:format])
respond_to do |format|
if @contributor.company.users.include?(current_user) && @contributor.company.contributors.find_by(user_id: current_user.id).manager? && @contributor.update(manager?: false)
collaborator = Contributor.where(company_id: @contributor.company.id, user_id: current_user.id).first
if Notification.create(notifiable: collaborator, action: "Você não é mais dono(a)", user_id: @contributor.user.id)
format.html { redirect_to companies_path, notice: 'Você retirou esta pessoa de dono(a)' }
format.json { head :no_content}
end
else
# format.html { redirect_to companies_path }
format.json { render json: @contributor.errors, status: :unprocessable_entity }
end
end
编辑 1
终端错误
Started PATCH "/contributors/make_manager.9" for 127.0.0.1 at 2019-05-18 00:09:35 -0300
Processing by ContributorsController#make_manager as
Parameters: {"authenticity_token"=>"YNXVM3w2GFpCrHzmJKbMSJa4LDE41wcZj2+I18hjwJi9yWh4BGto4n3MIKhfNSwxaC7+Gfy0fbMnlARmukw1Qw=="}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]]
Contributor Load (0.0ms) SELECT "contributors".* FROM "contributors" WHERE "contributors"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
Company Load (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" INNER JOIN "contributors" ON "users"."id" = "contributors"."user_id" WHERE "contributors"."company_id" = ? AND "users"."id" = ? LIMIT ? [["company_id", 8], ["id", 3], ["LIMIT", 1]]
Contributor Load (1.0ms) SELECT "contributors".* FROM "contributors" WHERE "contributors"."company_id" = ? AND "contributors"."user_id" = ? LIMIT ? [["company_id", 8], ["user_id", 3], ["LIMIT", 1]]
(0.0ms) begin transaction
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
SQL (1.0ms) UPDATE "contributors" SET "manager?" = ?, "updated_at" = ? WHERE "contributors"."id" = ? [["manager?", "t"], ["updated_at", "2019-05-18 00:09:35.776126"], ["id", 9]]
(6.0ms) commit transaction
Contributor Load (0.0ms) SELECT "contributors".* FROM "contributors" WHERE "contributors"."company_id" = ? AND "contributors"."user_id" = ? ORDER BY "contributors"."id" ASC LIMIT ? [["company_id", 8], ["user_id", 3], ["LIMIT", 1]]
(0.0ms) begin transaction
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "notifications" ("notifiable_type", "notifiable_id", "user_id", "action", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["notifiable_type", "Contributor"], ["notifiable_id", 7], ["user_id", 5], ["action", "Agora você é dono(a)"], ["created_at", "2019-05-18 00:09:35.795128"], ["updated_at", "2019-05-18 00:09:35.795128"]]
(6.0ms) commit transaction
Completed 406 Not Acceptable in 44ms (ActiveRecord: 17.0ms)
如您所见,它在错误之前创建了一切,所以我认为这只是 retunr 的问题
格式.html 格式.json
你问我参数,我认为发布这两个表的模式会很好
create_table "contributors", force: :cascade do |t|
t.integer "company_id"
t.integer "user_id"
t.boolean "manager?", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["company_id"], name: "index_contributors_on_company_id"
t.index ["user_id"], name: "index_contributors_on_user_id"
end
-----------------
create_table "notifications", force: :cascade do |t|
t.string "notifiable_type"
t.integer "notifiable_id"
t.integer "user_id"
t.string "action"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "read_at"
t.index ["notifiable_type", "notifiable_id"], name: "index_notifications_on_notifiable_type_and_notifiable_id"
t.index ["user_id"], name: "index_notifications_on_user_id"
end
和模型
class Contributor < ApplicationRecord
belongs_to :company
belongs_to :user
has_many :notifications, as: :notifiable
end
class Notification < ApplicationRecord
belongs_to :notifiable, polymorphic: true
belongs_to :user
end
解决方案
respond_to do |format|
if @contributor.company.users.include?(current_user) && @contributor.company.contributors.find_by(user_id: current_user.id).manager? && @contributor.update(manager?: false)
collaborator = Contributor.where(company_id: @contributor.company.id, user_id: current_user.id).first
if Notification.create(notifiable: collaborator, action: "Você não é mais dono(a)", user_id: @contributor.user.id)
format.html { redirect_to companies_path, notice: 'Você retirou esta pessoa de dono(a)' }
format.json { head :no_content}
end
else
format.html { redirect_to companies_path }
format.json { render json: @contributor.errors, status: :unprocessable_entity }
end
end
推荐阅读
- php - mysqli查询无法正常工作面临未知错误
- r - Windows批处理文件上带有空格的Rscript文件路径
- java - 如何更改 PDF 文件中图像的过滤器
- python - 在特定条件下聚合 Dataframe 中的行值
- linux - 使用 Bash 脚本通过 SSH 输出服务器上的所有文件夹
- java - 如何在响应正文中返回 WireMock 服务器端口
- css - 如何使“Reactjs Modal”出现在屏幕上从右到中心飞
- ffmpeg - ffmpeg fade 未应用于 atrim
- android - java.lang.IllegalArgumentException:平台类 java.time.LocalDateTime(没有注释)需要显式 JsonAdapter
- javascript - 这个增加/减少数字的 JavaScript 函数有哪些替代方法?