David
David

Reputation: 2139

Rails is not adding a new record

I am a newbie at rails

When I enter to a new record I

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

Upvotes: 0

Views: 47

Answers (1)

Gavin Miller
Gavin Miller

Reputation: 43875

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:

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

Upvotes: 3

Related Questions