0

我是 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

结尾

4

1 回答 1

3

为什么在每个字段中输入数据时数据显示为空白?

如果您指的是无法保存Jobwith 数据,那是因为在您的create操作中您正在构建您的Jobwith only params[:id],您需要使用完整的作业数据构建该对象。通常看起来像这样:

def create
  @job = Job.new(params[:job])
  # ...
end
于 2013-07-28T15:39:12.857 回答