Last active
July 14, 2016 18:33
-
-
Save Circuit8/5c546d0d12a17d114112e7c582e3cfd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Exercise < ApplicationRecord | |
has_one :image, :as =>:imageable | |
accepts_nested_attributes_for :image | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image < ApplicationRecord | |
belongs_to :imageable, polymorphic: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment