當您創建新作業時,您將被重定向到顯示頁面(/ jobs/[:id])。但是,當您嘗試重新加載頁面或編輯作業時,標題中會出現錯誤。有set_job,據我所知,它被正確設置,並被標記爲錯誤。'id'= 3時找不到作業
這裏的一個控制器:
class JobsController < ApplicationController
before_action :set_job, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show, :index]
def index
@jobs = Job.published.order('created_at DESC').paginate(:page => params[:page], :per_page => 20)
end
def show
end
def new
@job = Job.new
end
def edit
end
def create
@job = Job.new(job_params)
respond_to do |format|
if @job.save
format.html { redirect_to @job }
format.json { render :show, status: :created, location: @job }
else
format.html { render :new }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @job.update(job_params)
format.html { redirect_to @job }
format.json { render :show, status: :ok, location: @job }
else
format.html { render :edit }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
private
def set_job
@job = Job.find(params[:id])
end
def job_params
params.require(:job).permit(:name, :description, :image, :location, :deleted, :user_id, :tag_list)
end
end
日誌:
Job Load (0.3ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = $1 LIMIT 1 [["id", 3]]
Completed 404 Not Found in 74ms
ActiveRecord::RecordNotFound (Couldn't find Job with 'id'=3):
app/controllers/jobs_controller.rb:56:in `set_job'
日誌部2
Started POST "/jobs" for 188.246.77.192 at 2014-09-20 22:11:03 +0000
Processing by JobsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8gO/Q8ktERzKdMzDtB8KEU6CH8oIayNAb2AZhUzL2d0=", "job"=>{"name"=>"My first job", "description"=>"test", "tag_list"=>"rails, programmi
ng", "location"=>"Red River, United States", "user_id"=>"1"}, "_wysihtml5_mode"=>"1", "commit"=>"Submit"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "updated_at" = '2014-09-20 22:11:03.154670', "last_seen_at" = '2014-09-20 22:11:03.154670' WHERE "users"."id" = 1
(4.3ms) COMMIT
(0.2ms) BEGIN
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
(0.3ms) SELECT COUNT(*) FROM "jobs" WHERE "jobs"."user_id" = $1 AND ("jobs"."created_at" BETWEEN '2014-09-20 00:00:00.000000' AND '2014-09-20 22:11:03.168092') [["user_id", 1]]
SQL (0.3ms) INSERT INTO "jobs" ("created_at", "description", "location", "name", "tag_list", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["create
d_at", "2014-09-20 22:11:03.170612"], ["description", "test"], ["location", "Red River, United States"], ["name", "My first job"], ["tag_list", "rails, programming"], ["updated_at",
"2014-09-20 22:11:03.170612"], ["user_id", 1]]
(3.0ms) COMMIT
Redirected to http://example.com/jobs/3
Completed 302 Found in 27ms (ActiveRecord: 9.6ms)
我認爲您應該先檢查是否存在id = 3的作業。轉到您的終端,瀏覽你的應用程序中,然後運行: 軌道ç 然後運行下面的檢查與ID 3工作存在: Job.find(3) – 2014-09-20 21:51:12
它不存在。奇怪的。當我創建一個記錄時,我被重定向到一個演出頁面。現在,當我創建一個新紀錄時,它是#4。你覺得怎麼樣? – 2014-09-20 21:53:23
這個記錄是否真的被存儲在表中?在檢查記錄時檢查3.如果不是,那麼創建操作時可能存在問題。在這種情況下,你可以打印創建行動的參數並將其放在這裏。 – 2014-09-20 22:06:25