简体   繁体   中英

ActionController::UnknownFormat (ActionController::UnknownFormat)

Well, i'm trying to update some register from database, but it keeps raising the unknownFormat erros, do you guys see what am i missing?

It kind of is pretty basic, but i feel like i'm missing somthing really important here

 @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 

Edit 1

the error in terminal


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)


As you see, it creates everithing before the error, so i think it is just a problem with the retunr

format.html format.json

You asked me about the parameter, i think it would be nice to post the schemas of this 2 tables


  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

And the models

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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