ActionController :: UnknownFormat(ActionController :: UnknownFormat)

时间:2019-05-18 03:15:28

标签: ruby-on-rails ruby-on-rails-5

好吧,我正在尝试从数据库中更新一些寄存器,但是它一直在引发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

您问我有关参数的问题,我认为发布这2个表的架构会很好


  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

1 个答案:

答案 0 :(得分:0)

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