2014-09-20 68 views
0

當您創建新作業時,您將被重定向到顯示頁面(/ 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) 
+0

我認爲您應該先檢查是否存在id = 3的作業。轉到您的終端,瀏覽你的應用程序中,然後運行: 軌道ç 然後運行下面的檢查與ID 3工作存在: Job.find(3) – 2014-09-20 21:51:12

+0

它不存在。奇怪的。當我創建一個記錄時,我被重定向到一個演出頁面。現在,當我創建一個新紀錄時,它是#4。你覺得怎麼樣? – 2014-09-20 21:53:23

+0

這個記錄是否真的被存儲在表中?在檢查記錄時檢查3.如果不是,那麼創建操作時可能存在問題。在這種情況下,你可以打印創建行動的參數並將其放在這裏。 – 2014-09-20 22:06:25

回答

0

工作

底部利ne是你的數據庫中沒有Jobid3 - 修復它的方法將來自於確定爲什麼這確實會被填充。

具體而言,您將遇到save方法或database問題。這裏是我會做什麼來解決它:

#app/controllers/jobs_controller.rb 
class JobsController < AppicationController 
    def new 
     @job = Job.new 
    end 

    def create 
     @job = Job.new job_params 
     @job.save 
    end 

    private 

    def job_params 
     require(:job).permit(:x, :y, :z) 
    end 
end 

你已經擁有的控制器正確的 - 有可能因此與該數據被保存在數據庫中的方法問題。這可能是您的model或您的save機制本身的問題。

這是你的模型應該是什麼樣子:

#app/models/job.rb 
class Job < ActiveRecord::Base 
    belongs_to :user 
end 

#app/models/user.rn 
class User < ActiveRecord::Base 
    has_many :jobs 
end 

這將確保您的型號沒有引起問題。有可能是另一個問題對於validations你有 - 你應該評論任何你Job模型驗證的,只是爲了檢查是否正確提交

-

DB

您應該進一步確保您有正確的db爲您的Jobmodel

要做到這一點,你需要ŧ o確保你已經完成了以下內容:

$耙分貝:遷移

這是當然的,如果你已經創建了正確的遷移之前爲你的模型!


最後,你就可以使用rails console查看在您的分貝job記錄的存在:

> $ rails c 
> $ jobs = Job.all 
> # -> all jobs outputted 

這個,你就可以看到你的記錄有已被插入,因此您可以查看已保存的記錄發生了什麼