レコードの作成または存在するかどうかの確認に問題があります。私は "チェック"コントローラに "id"と "uid"を作成するために2つのエントリを提出しています。下記の移行:ActiveRecord - レコードを作成しましたが、存在するかどうかを確認すると何も返されません。
create_checks
class CreateChecks < ActiveRecord::Migration[5.0]
def change
create_table :checks do |t|
t.timestamps
end
end
end
add_uid_to_checks
class AddUidToChecks < ActiveRecord::Migration[5.0]
def change
add_column :checks, :uid, :string
end
end
Checks_controller#私に与え
def create
@check = Check.new(check_params)
respond_to do |format|
if @check.save
format.html { redirect_to @check, notice: 'Check was successfully created.' }
format.json { render :show, status: :created, location: @check }
else
format.html { render :new }
format.json { render json: @check.errors, status: :unprocessable_entity }
end
end
end
を作成する二つのパラメータでAJAXを通じて提出する場合は、次の - IDおよびUID
Started POST "/checks.json" for ::1 at 2016-09-05 09:36:31 -0500
Processing by ChecksController#create as JSON
Parameters: {"id"=>"54545454", "uid"=>"23232323"}
(0.0ms) begin transaction
SQL (2.0ms) INSERT INTO "checks" ("created_at", "updated_at") VALUES (?, ?)
[["created_at", 2016-09-05 14:36:31 UTC], ["updated_at", 2016-09-05 14:36:31 UTC
]]
(10.0ms) commit transaction
Rendering checks/show.json.jbuilder
Rendered checks/_check.json.jbuilder (1.0ms)
Rendered checks/show.json.jbuilder (9.0ms)
Completed 201 Created in 201ms (Views: 174.8ms | ActiveRecord: 12.0ms)
その後、私はそれがこのコントローラを使用して存在するかどうかを確認...
def set_check
if Check.exists?(params[:id])
render plain: "1"
else
render plain: "0"
end
end
Started GET "/checks/54545454" for ::1 at 2016-09-05 0
9:36:37 -0500
Processing by ChecksController#show as */*
Parameters: {"id"=>"54545454"}
Check Exists (1.0ms) SELECT 1 AS one FROM "checks" WHERE "checks"."id" = ? L
IMIT ? [["id", 0], ["LIMIT", 1]]
Rendering text template
Rendered text template (0.0ms)
Filter chain halted as :set_check rendered or redirected
Completed 200 OK in 7ms (Views: 1.8ms | ActiveRecord: 1.0ms)
私を与え
...そしてレンダリング "0" が見つかりません。私は何か間違っていると確信していますが、どこを特定するのか困っています。
以上のコードビット:
routes.rbを関連セクション:set_checksが
ルートコードと 'set_check'が呼び出される場所を追加してください。 – Ven