Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refinery::Image delete in overview => Error on association #2489

Closed
mattherick opened this issue Dec 6, 2013 · 7 comments
Closed

Refinery::Image delete in overview => Error on association #2489

mattherick opened this issue Dec 6, 2013 · 7 comments

Comments

@mattherick
Copy link
Contributor

I have an own refinery extension, so far so good and everything works.
I have a Project which has a logo => association to Refinery::Image

    module Refinery
      module Projects
        class Project < Refinery::Core::BaseModel
          self.table_name = 'refinery_projects'
          belongs_to :logo, :class_name => '::Refinery::Image'
          attr_accessible :logo_id
          validates :logo_id, :presence => true
        end
      end
    end

In the Refinery::Images overview I can edit, update and destroy the image, so far so good.

But no my problem is, that I have validates :presence => true on my project logo. And If I have an image_tag in my views like:

<%= image_tag @project.logo.url %> 

And the image was destroyed on localhost:3000/refinery/images I get an exception

undefined method `url' for nil:NilClass

because of the missing image of the association.

Is this a bug or a feature?

Of course I could fix the view exception like:

<%= image_tag @project.logo.url if @project.logo %> 

but that is annoying, because of the presence validation.

@parndt
Copy link
Member

parndt commented Dec 6, 2013

That makes sense.. if you delete an image it's going to be nil?

@mattherick
Copy link
Contributor Author

Yes it makes sense and also not :-). Is there a workaround to fix that?

@parndt
Copy link
Member

parndt commented Dec 6, 2013

The workaround you suggested, yes.

@mattherick
Copy link
Contributor Author

Why not fix this within refinery? For example, if an image is associated to something, it is not deletable? A before filter for example "This image is associated to.., not deletable".. ?

@parndt
Copy link
Member

parndt commented Dec 7, 2013

How does the image know what it's associated with?

@mattherick
Copy link
Contributor Author

Yes that's the problem. Maybe it could be fixed within the refinery-images gem. For own extensions everybody must fixed it individually...hmm.

@parndt
Copy link
Member

parndt commented Dec 8, 2013

Currently other models own the foreign key relationship with images and we'd have to flip that around the other way to implement what you're after.
So, an image would have to has_many :records_from_other_models and it'd need to be polymorphic.

@parndt parndt closed this as completed Jul 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants