Skip to content

Instantly share code, notes, and snippets.

@cdesch
Last active September 18, 2017 15:23
Show Gist options
  • Save cdesch/ba707f7d5229e314a9d1f55b0010ca79 to your computer and use it in GitHub Desktop.
Save cdesch/ba707f7d5229e314a9d1f55b0010ca79 to your computer and use it in GitHub Desktop.

The migration is an easy one line fix:

class CreateCaseStudies < ActiveRecord::Migration[5.1]
  def change
    create_table :case_studies do |t|
      t.string :title
      t.text :description
      t.text :short_description
      t.string :slug
      t.text :content
      t.boolean :hidden, default: false
      t.datetime :published_at
      t.string :seo_tags
      t.seo_description
      t.string :image

      t.timestamps
    end
    add_index :case_studies, :slug, unique: true
  end
end

Let's change:

t.seo_description to t.string :seo_description

Cool, that was easy, but there is more looking at the FactoryGirl spec is easy to fix:

FactoryGirl.define do
  factory :case_study do
    title "MyString"
    description "MyText"
    short_description "MyText"
    slug "MyString"
    content "MyText"
    hidden false
    published_at "2017-09-18 09:04:17"
    seo_tags "MyString"
    ""
    image "MyString"
  end
end

Let's now change:

"" to seo_description "MyString"

Okay, that one was easy to fix too. No biggie, we can do this manually as the Controller looks like an easy fix too inside the strong parameters private method:

private
  # Use callbacks to share common setup or constraints between actions.
  def set_case_study
    @case_study = CaseStudy.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def case_study_params
    params.require(:case_study).permit(:title, :description, :short_description,
                                       :slug, :content, :hidden, :published_at,
                                       :seo_tags, :, :image)
  end

Let's change from:

params.require(:case_study).permit(:title, :description, :short_description,
                                       :slug, :content, :hidden, :published_at,
                                       :seo_tags, :, :image)

to

params.require(:case_study).permit(:title, :description, :short_description,
 :slug, :content, :hidden, :published_at,
 :seo_tags, :seo_description, :image)

We are well on our way to fixing our mistake, but oh crap, the views! There are a bunch of those files and I haven't done anything with them. I don't want to have to edit all of them! And I almost forgot, the view specs too! Well, the view specs will probably be completely ripped out, but I really don't want my tests to start failing immediately. It would be nice to get those taken care of.

Wouldn't it be nice if we could just rerun the generator for only the views? Sarcasm ... We can!!

First, lets fix the generator we botched earlier:

rails g scaffold CaseStudy title:string description:text short_description:text slug:string:uniq content:text hidden:boolean published_at:datetime seo_tags:string :seo_description:string image:string -c=scaffold_controller

to

rails g scaffold CaseStudy title:string description:text short_description:text slug:string:uniq content:text hidden:boolean published_at:datetime seo_tags:string seo_description:string image:string -c=scaffold_controller

Now delete the views/case_studies directory containing the bad views and run the scaffold command to only generate the views

rails g erb:scaffold  CaseStudy title:string description:text short_description:text slug:string:uniq content:text hidden:boolean published_at:datetime seo_tags:string seo_description:string image:string --skip

Lets do the same for the specs:

rails g rspec:view case_studies index edit new show

Now, lets commit our code and back to what we were working before we realized our mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment