0

I am building a database of skills and in the beginning thinking that I would need a title param for them I added it as a required param. I would like to get rid of the title param all together so that I can just create new skills and a description. Right now there is an error because it is also looking for a :title. Here is my controller:

class SkillsController < ApplicationController



  def index
    @skills = Skill.all
   end

   def show
    @skills = Skill.all
   end

   def new
    @skills = Skill.all

   end

 def create
  @skills = Skill.new(skill_params)
  if @skills.save
    redirect_to :action => 'index'
  else
    @skills = Skill.find(:all)
    render :action => 'new'
  end
end

   def edit
    @skills = Skill.find(params[:id])
    @skills = Skill.find(:all)
   end

   def update
       @skills = Skill.find(params[:id])
      if @skills.update_attributes(params[:skill])
         redirect_to :action => 'show', :id => @skills
      else
         @skills = Skill.find(:all)
         render :action => 'edit'
      end
   end

   def delete
    Skill.find(params[:id]).destroy
    redirect_to :action => 'index'
   end

     def show_skills
      @skills = Skill.find(params[:id])
   end
end

private

  def skill_params
    params.require(:skill).permit(:attribute_1, :attribute_2, :attribute_3)
  end

and here is my error when I try and submit a new skill:

ActiveRecord::StatementInvalid in SkillsController#create
SQLite3::ConstraintException: skills.title may not be NULL: INSERT INTO "skills" ("created_at") VALUES (?)

I think the easiest way to bypass this issue is to make skill.title not required but I am not sure for certain how exactly to fix it. I will also post my /new form if that helps:

<h1>Add new Skill</h1>
<%= form_tag ({action: "create"}) do %>
<p><label for="skill">Skill</label>:
<%= text_field 'skill', 'title' %></p>

<p><label for="skill_description">Description</label><br/>
<%= text_area 'skill', 'description' %></p>
<%= submit_tag "Create" %>
<% end  %>
<%= link_to 'Back', {:action => 'index'} %> 

I tried getting rid of the 'title' line and it made even more problems. I will keep messing with the code and researching and thank you anyone who knows anything about my issue. Cheers and thanks again stackers!!!

ZachyBear
  • 297
  • 3
  • 15

2 Answers2

0

Looks like you created the title column originally with a NOT NULL constraint, so the database won't allow the record to be created without a title.

If you truly don't need it, then you should drop the column from the database.

Create a migration:

bundle exec rails generate migration remove_title_from_skills

Edit the migration file created and add:

def change
  remove_column :skills, :title
end

Run the migration:

bundle exec rake db:migrate

Remove all other references to :title in your application.

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
0

Well,then as i said,your skill_params should be like this

def skill_params

params.require(:skill).permit(:title,:description)

end

You should be permitting your attributes in the list of permitted params.

For more info,you should be looking at Strong Parameters.

Pavan
  • 33,316
  • 7
  • 50
  • 76