Skip to content

Instantly share code, notes, and snippets.

@Circuit8
Last active July 14, 2016 18:33
Show Gist options
  • Select an option

  • Save Circuit8/5c546d0d12a17d114112e7c582e3cfd9 to your computer and use it in GitHub Desktop.

Select an option

Save Circuit8/5c546d0d12a17d114112e7c582e3cfd9 to your computer and use it in GitHub Desktop.

Revisions

  1. Circuit8 revised this gist Jul 14, 2016. 2 changed files with 23 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Error Message
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    ActiveRecord::RecordInvalid: Validation failed: Image imageable must exist
    23 changes: 23 additions & 0 deletions pry.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    [25] pry(main)> exercise = Exercise.new(:name =>"anything")
    => #<Exercise:0x0055af50e06870
    id: nil,
    name: "anything",
    created_at: nil,
    updated_at: nil,
    exercise_category_id: nil>
    [26] pry(main)> exercise.build_image(:file_file_name => "anything")
    => #<Image:0x0055af50d806f8
    id: nil,
    title: "",
    imageable_type: "Exercise",
    imageable_id: nil,
    position: 1,
    created_at: nil,
    updated_at: nil,
    file_file_name: "anything",
    file_content_type: nil,
    file_file_size: nil,
    file_updated_at: nil>
    [27] pry(main)> exercise.save!
    ActiveRecord::RecordInvalid: Validation failed: Image imageable must exist
    from /home/joshua/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/validations.rb:78:in `raise_validation_error'
  2. Circuit8 created this gist Jul 14, 2016.
    1 change: 1 addition & 0 deletions Error Message
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ActiveRecord::RecordInvalid: Validation failed: Image imageable must exist
    4 changes: 4 additions & 0 deletions Exercise.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    class Exercise < ApplicationRecord
    has_one :image, :as =>:imageable
    accepts_nested_attributes_for :image
    end
    17 changes: 17 additions & 0 deletions Exercise_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # This is failing
    describe 'saving image' do
    context 'with an associated image' do

    let :exercise do
    build :exercise
    end

    before :each do
    exercise.build_image(attributes_for(:image))
    end

    it 'should save correctly with an associated image' do
    expect{ exercise.save! }.to_not raise_error
    end
    end
    end
    22 changes: 22 additions & 0 deletions ExercisesController.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    class Admin::ExercisesController < Admin::BaseController
    def new
    @exercise = Exercise.new
    @exercise.build_image
    end

    def create
    @exercise = Exercise.new(exercise_params)
    respond_to do |format|
    if @exercise.save
    format.html { redirect_to admin_exercises_url, notice: 'Exercise was successfully created.' }
    else
    format.html { render :new }
    end
    end
    end

    private
    def exercise_params
    params.require(:exercise).permit(:name, :exercise_category_id, :image_attributes => [:file])
    end
    end
    3 changes: 3 additions & 0 deletions Image.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    class Image < ApplicationRecord
    belongs_to :imageable, polymorphic: true
    end