When you create new job, you'll get redirected to show page (/jobs/[:id]). But, when you try to reload a page or edit a job, you'll get an error in the title. There is set_job, which is set properly as far as I see and that is marked as an error.
Here's a controller:
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
logs:
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'
logs part 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)
Job
The bottom line is that you'll not have a Job
in your database with an id
of 3
- the way to fixing it will come from determining why this does get populated.
Specifically, that you'll either have a problem with the save
method or your database
. Here's what I would do to resolve it:
#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
You already have the controller right - the problem with therefore likely be with the way this data is being saved in your database. This will either be a problem with your model
, or your save
mechanism itself.
Here's what your model should look like:
#app/models/job.rb
class Job < ActiveRecord::Base
belongs_to :user
end
#app/models/user.rn
class User < ActiveRecord::Base
has_many :jobs
end
This will ensure your models are not causing an issue. There might be another issue in respect to the validations
you have - you should comment out any of the validations in your Job
model, just to check if it submits correctly
--
DB
You should further ensure that you have the correct db
set up for your Job
model
To do this, you need to ensure you have done the following:
$ rake db:migrate
This is of course if you have created the correct migrations previously for your model!
Finally, you'll be able to check the presence of the job
record in your db using the rails console
:
> $ rails c
> $ jobs = Job.all
> # -> all jobs outputted
From this, you'll be able to see where your records have been inserted, thus allowing you to see what's happened to the records you've saved
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.