Rails is not adding a new record

时间:2021-02-06 15:39:47

I am a newbie at rails

我是rails的新手

When I enter to a new record I

当我进入一个新的记录我

  • Job title can't be blank
  • 职称不能为空

  • Job category can't be blank
  • 工作类别不能为空

  • Job type can't be blank
  • 作业类型不能为空

Why is the data showing as blank when I am entering data in every field.

当我在每个字段中输入数据时,为什么数据显示为空白。

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

end

1 个解决方案

#1


3  

Why is the data showing as blank when I am entering data in every field?

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

If you're refering to being unable to save a Job with data, it's because in your create action you're building your Job with only params[:id], you'll need to build that object with the full job data. Typically that will look like this:

如果您指的是无法使用数据保存作业,那是因为在您的创建操作中,您只使用params [:id]构建作业,您需要使用完整的作业数据构建该对象。通常情况如下:

def create
  @job = Job.new(params[:job])
  # ...
end

#1


3  

Why is the data showing as blank when I am entering data in every field?

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

If you're refering to being unable to save a Job with data, it's because in your create action you're building your Job with only params[:id], you'll need to build that object with the full job data. Typically that will look like this:

如果您指的是无法使用数据保存作业,那是因为在您的创建操作中,您只使用params [:id]构建作业,您需要使用完整的作业数据构建该对象。通常情况如下:

def create
  @job = Job.new(params[:job])
  # ...
end