我是 Rails 的新手
当我进入新记录时,我
- 职位名称不能为空
- 职位类别不能为空
- 作业类型不能为空
为什么我在每个字段中输入数据时数据显示为空白。
class Job < ActiveRecord::Base
attr_accessible :job_title, :job_category, :job_type
validates :job_title, presence: true
validates :job_category, presence: true
validates :job_type, presence: true
end
class JobsController < ApplicationController
def show
@job = Job.find(params[:id])
end
def new
@job = Job.new
end
def edit
@user = Job.find(params[:id])
end
def create
@job = Job.new(params[:id])
if @job.save
redirect_to @job
flash[:success] = "New Job Added! "
else
render 'new'
end
end
end
job_spec.rb
require 'spec_helper'
describe Job do
before do
@job = Job.new(job_title: "Structural Engineer", job_category: "Civil engineer", job_type: "Engineeer")
end
subject { @job }
it { should respond_to(:job_title) }
it { should respond_to(:job_category) }
it { should respond_to(:job_type) }
it { should be_valid }
describe "when job_title is not present" do
before { @job.job_title = " " }
it { should_not be_valid }
end
describe "when job_category is not present" do
before { @job.job_category = " " }
it { should_not be_valid }
end
describe "when job_type is not present" do
before { @job.job_type = " " }
it { should_not be_valid }
end
describe "when job_title is already taken" do
before do
job_with_same_job_title = @job.dup
job_with_same_job_title = @job.job_title.upcase
end
it { should_not be_valid }
end
结尾