Form_for @object with carrierwave upload form

How do i get the values for imageable_id and imageable_type inserted
into a
new picture object? i can put imageable_type as a hidden field, but
imageable_id i don’t know

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.

W dniu 23.01.2016 o 03:17, fugee ohu pisze:

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 ?

This is a broader issue than just file uploads anyway so I need to learn
this, if you read the link I posted they make carrierwave sound good so
i
wanna get comfortable with using it

On Thursday, January 21, 2016 at 11:16:40 PM UTC-5, fugee ohu wrote:

<%= f.submit %>

<% end %>

I’m staying the course with carrierwave because I wanna learn to use it
because of what I read here

On Jan 22, 2016, at 9:40 PM, fugee ohu [email protected] wrote:

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:

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