繁体   English   中英

ActionController :: UnknownFormat(ActionController :: UnknownFormat)

[英]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的一个问题

format.html format.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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM