form_for @object with carrierwave upload form

Are you using Paperclip or other gem ? I’m asking because Paperclip resolved this issue and you don’t have think about this other fields and values.

I’m using carrierwave In my controller I can test for the presence of params[profile_id] and then i would know the value of imageable_type should be set to profile but how do i pass a value for imageable_id ?

I’m staying the course with carrierwave because I wanna learn to use it because of what I read here Best Rails Image Uploader Paperclip vs. Carrierwave vs. Refile | Infinum

I'm using carrierwave In my controller I can test for the presence of params[profile_id] and then i would know the value of imageable_type should be set to profile but how do i pass a value for imageable_id ?

It sounds as though you are using a polymorphic relation here. You don't need to set either of these fields manually, Rails will do it for you. You create a related object by assigning the object to the relation, and Rails takes care of all of the plumbing. Here's an example (swiped from the Association Basics guide, which you may want to read).

class Picture < ActiveRecord::Base   belongs_to :imageable, polymorphic: true end

class Employee < ActiveRecord::Base   has_many :pictures, as: :imageable end

class Product < ActiveRecord::Base   has_many :pictures, as: :imageable end

If you are adding the picture to a product, through the products_controller, then you would have a nested form, and the #new method would include something like this:

def new   @product = Product.new   @product.pictures.build end

That builds an associated record in memory so your form can render a field for it.

... product fields here ... <%= fields_for :pictures do |p| %> <%= p.file_field :file %> <%- end -%> ... rest of the product form here ...

More about nested forms here: Action View Form Helpers — Ruby on Rails Guides

Really, you shouldn't see any case where you would need to manually add the value of the imageable_type and imageable_id anywhere here, because that's an implementation detail that Rails takes care of for you. In general, when you find yourself trying to figure out how to set these sorts of things, you are working too hard, and that is a sign that you have gone "off the rails". Look around for the way to do it without all that effort, and your code (and life) will be much better.

Also, you can do this from the other direction, through a pictures_controller, and you would simply need to set the @picture.product to an actual product, or a @picture.employee to an actual employee, and again the imageable details would be wired up for you.

Walter