简体   繁体   English

Rails没有添加新记录

[英]Rails is not adding a new record

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 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 结束

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. 如果你指的是无法保存Job的数据,这是因为在你create行动你建立你的Job ,只有params[:id] ,你需要建立一个对象,具有完整的作业数据。 Typically that will look like this: 通常情况如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM