首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionController::UnknownFormat (ActionController::UnknownFormat)

ActionController::UnknownFormat (ActionController::UnknownFormat)
EN

Stack Overflow用户
提问于 2019-05-18 11:15:28
回答 1查看 65关注 0票数 0

嗯,我正在尝试从数据库中更新一些寄存器,但它一直在引发unknownFormat错误,你们看到我错过了什么吗?

这是很基本的,但我觉得我遗漏了一些非常重要的东西

代码语言:javascript
复制
 @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

终端中的错误

代码语言:javascript
复制
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

你问我关于参数的问题,我想把这两个表的模式发布出来会很好

代码语言:javascript
复制
  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

和模型

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-05-18 12:12:49

代码语言:javascript
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56195687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档