创建新作业时,您将被重定向到显示页面(/ jobs / [:id])。但是,当您尝试重新加载页面或编辑作业时,标题中会出现错误。有set_job,就我看来而言设置正确,并标记为错误。
这是一个控制器:
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 :(得分:0)
<强>作业强>
最重要的是,您的数据库中没有Job
id
3
- 修复它的方法将来自确定为什么这会填充。
具体而言,您可能会遇到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
Job
model
为此,您需要确保已完成以下操作:
$ rake db:migrate
当然,如果您之前已为模型创建了正确的迁移,那么这就是
最后,您可以使用job
检查数据库中rails console
记录的存在:
> $ rails c
> $ jobs = Job.all
> # -> all jobs outputted
通过这种方式,您可以查看记录的插入位置,从而可以了解您已保存的记录发生了什么