So in my config/routes.rb I have :
map.resources :product_lines, :has_many =>
[:products, :documents], :has_one => :image
Note: Images are polymorphic (imageable)
In my images controller I have:
def new
@imageable = polymorphic_parent(params)
@image = Image.new
…
The polymorphic_parent function just looks through the params and
finds one with _id and finds the parent instance of the polymorphic
relationship. So, in app/controllers/application.rb I have:
def polymorphic_parent(params)
for key, param in params
if key.match(/_id$/)
klass = key[0…key.length - 3].classify
return eval("#{klass}.find(param)")
end
end
end
And in my app/views/images/new.html.erb I have:
<% form_for([@imageable, @image], :html => { :multipart => true }) do |
f| %>
… etc…
But it throws and error because I guess polymorphic_routes doesn’t
work with has_one…
undefined method `product_line_images_path’ for #<ActionView::Base:
0xb620f970>
I think the path should be product_line_image_path (image not images).
Does anybody know If I can make this work? Am I doing something
wrong?
Thanks