> On Jan 22, 2016, at 9:40 PM, fugee ohu <fugee...@gmail.com> 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: 
http://guides.rubyonrails.org/form_helpers.html#nested-forms

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


> 
> On Friday, January 22, 2016 at 9:20:04 PM UTC-5, Przemek Kosakowski wrote:
> 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:
>> 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
>> 
>> On Thursday, January 21, 2016 at 11:16:40 PM UTC-5, fugee ohu wrote:
>> This form raises undefined method `pictures_path' for 
>> #<#<Class:0xaea3ad0>:0xbb9cb4c>
>> 
>> <%= form_for(@picture, :html => { :multipart => true }) do |f| %>
>> 
>>   <p>
>>     <%= f.file_field :image %>
>>   </p>
>> 
>>   <p><%= f.submit %></p>
>> 
>> <% end %>
>> 
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to rubyonrails-ta...@googlegroups.com.
>> To post to this group, send email to rubyonra...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rubyonrails-talk/920d481a-fc83-443f-afb6-99e20e1863ff%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/24627943-9494-4959-8d57-8726a6627122%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/DEF1164D-19BD-44A8-A218-F02C40C4A042%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to