form_for @object with carrierwave upload form

34 views
Skip to first unread message

fugee ohu

unread,
Jan 21, 2016, 11:16:40 PM1/21/16
to Ruby on Rails: Talk
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 %>


Przemek Kosakowski

unread,
Jan 21, 2016, 11:40:30 PM1/21/16
to rubyonra...@googlegroups.com
Hello,

can you show your model and controller file ?

W dniu 22.01.2016 o 05:16, fugee ohu pisze:
--
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/345bb1b7-2849-4271-92e3-59c228d2c596%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fugee ohu

unread,
Jan 22, 2016, 4:40:59 AM1/22/16
to Ruby on Rails: Talk


On Thursday, January 21, 2016 at 11:40:30 PM UTC-5, Przemek Kosakowski wrote:
Hello,

can you show your model and controller file ?
class PicturesController < ApplicationController
  def index
    @pictures = Picture.all
  end

  def new
   @picture = current_user.profile.pictures.new
  end

  def create
    @picture = Picture.new(picture_params)

    if @picture.save
      redirect_to current_user_profile_pictures_path, notice: "The picture #{@picture.name} has been uploaded."
    else
      render "new"
    end
  end

  def destroy
    @picture = Picture.find(params[:id])
    @picture.destroy
    redirect_to current_user_profile_pictures_path, notice:  "The picture #{@picture.name} has been deleted."
  end
=============================================================================
class Picture < ActiveRecord::Base
 belongs_to :imageable, polymorphic: true
 mount_uploader :imageable, PictureUploader
 validates :name, presence: true
end

private
  def picture_params
    params.require(:picture).permit(:name, :attachment)
  end
end
 

Przemek Kosakowski

unread,
Jan 22, 2016, 4:59:26 AM1/22/16
to rubyonra...@googlegroups.com
I see you trying upload some picture, please use this gem [1]. You be able in easy way customize your picture uploader.

[1]:
paperclip

Have you relation between user -> profile -> picture ?

W dniu 22.01.2016 o 10:40, fugee ohu pisze:

fugee ohu

unread,
Jan 22, 2016, 5:16:37 AM1/22/16
to Ruby on Rails: Talk
OK But now, the subject is carrierwave Users has_one profile; Profile has_many pictures as :imageable I'm totally lost, I dunno what role in the upload the controller's supposed to play since i have the uploader.rb

Colin Law

unread,
Jan 22, 2016, 5:16:55 AM1/22/16
to Ruby on Rails: Talk
On 22 January 2016 at 04:16, fugee ohu <fuge...@gmail.com> wrote:
> This form raises undefined method `pictures_path' for
> #<#<Class:0xaea3ad0>:0xbb9cb4c>
>
> <%= form_for(@picture, :html => { :multipart => true }) do |f| %>

Have you defined that route in routes.rb (via resources: pictures for example)?

I agree with Przemek that you would probably be better off using
paperclip for uploading. It makes life very easy.

Colin

fugee ohu

unread,
Jan 22, 2016, 5:20:45 AM1/22/16
to Ruby on Rails: Talk

I wouldn't disagree   Here's the route:       post 'users/:id/profiles/:id/pictures/new' => 'pictures#new'

Przemek Kosakowski

unread,
Jan 22, 2016, 5:28:03 AM1/22/16
to rubyonra...@googlegroups.com
Look on below situation:

class User < ActiveRecord::Base
    has_one :profile, :autosave => true
end

class Profile < ActiveRecord::Base
    belongs_to :user
    has_attached_file :picture,
                    :styles => { medium: "300x200>", thumb: "100x100>" },
                      :storage => :s3,
                      :s3_credentials => Proc.new{|a| a.instance.s3_credentials}
    validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/
end

this two class + paperclip and that's enough. I think You don't need extra model for keeping user profile picture, you can easily add column "picture" in Your Profile model.


W dniu 22.01.2016 o 11:20, fugee ohu pisze:
--
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.

nanaya

unread,
Jan 22, 2016, 5:39:39 AM1/22/16
to rubyonra...@googlegroups.com
Hi,

On Fri, Jan 22, 2016, at 19:20, fugee ohu wrote:
> On Friday, January 22, 2016 at 5:16:55 AM UTC-5, Colin Law wrote:
> >
> > On 22 January 2016 at 04:16, fugee ohu <fuge...@gmail.com <javascript:>>
> > wrote:
> > > This form raises undefined method `pictures_path' for
> > > #<#<Class:0xaea3ad0>:0xbb9cb4c>
> > >
> > > <%= form_for(@picture, :html => { :multipart => true }) do |f| %>
> >
> > Have you defined that route in routes.rb (via resources: pictures for
> > example)?
> >
> > I agree with Przemek that you would probably be better off using
> > paperclip for uploading. It makes life very easy.
> >
> > Colin
> >
>
> I wouldn't disagree Here's the route: post
> 'users/:id/profiles/:id/pictures/new' => 'pictures#new'
>

That route doesn't add the `pictures_path` named route. Try adding `as:
'pictures'` option.

Also note that neither the action name nor route you specified follows
rails standard convention[1].

[1] http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

fugee ohu

unread,
Jan 22, 2016, 5:59:16 AM1/22/16
to Ruby on Rails: Talk

  thanks, add column picture to the profile model? profiles can have many pictures

Przemek Kosakowski

unread,
Jan 22, 2016, 6:01:33 AM1/22/16
to rubyonra...@googlegroups.com
many pictures you mean "a few different user profile photo" or you mean "few copy of the same picture but different size/dimension" ?

W dniu 22.01.2016 o 11:59, fugee ohu pisze:
--
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.

fugee ohu

unread,
Jan 22, 2016, 6:06:40 AM1/22/16
to Ruby on Rails: Talk

 Many pictures, not just different versions of the same picture

Przemek Kosakowski

unread,
Jan 22, 2016, 6:11:34 AM1/22/16
to rubyonra...@googlegroups.com
Many pictures for user profile ? I can't agree but okay. If you have many pictures for one user then you should create Gallery model instead of Picture. Even on fb user be able to have only one profile picture.

But I don't judge, maybe your solution is effective.


W dniu 22.01.2016 o 12:06, fugee ohu pisze:

Colin Law

unread,
Jan 22, 2016, 6:34:18 AM1/22/16
to Ruby on Rails: Talk
#new is the method used for showing the form, and is a get not a post,
form_for needs the route for posting to the create method. If you
don't supply a url in the call of form_for it assumes pictures_path
which must have been provided using resources: pictures for at least
the create method. See [1] for details. I expect that is described
in the Rails Guides also.

[1] http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

Colin

fugee ohu

unread,
Jan 22, 2016, 11:14:51 AM1/22/16
to Ruby on Rails: Talk

Yes, I'm using bootstrap-image-gallery (an extension of bluimp) Where would I see the table definition for Gallery model?
 
Message has been deleted

fugee ohu

unread,
Jan 22, 2016, 12:42:10 PM1/22/16
to Ruby on Rails: Talk
On Thursday, January 21, 2016 at 11:16:40 PM UTC-5, fugee ohu wrote:

And how is the file supposed to actually get uploaded, i changed the create action in my pictures controller to look like this but the file didn't get uploaded


  def create
    @picture = Picture.new(picture_params)
    if @picture.save
      uploader = PictureUploader.new
      picture = params[:imageable]
      uploader.store!(picture)
      redirect_to user_profile_path(current_user.id, current_user.profile.id), notice: "The picture #{@picture.name} has been uploaded."

fugee ohu

unread,
Jan 22, 2016, 1:55:49 PM1/22/16
to Ruby on Rails: Talk


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

the docs for paperclip describe as being used for attachments, in my case i'm just uploading files not as attachments

Walter Lee Davis

unread,
Jan 22, 2016, 5:15:10 PM1/22/16
to rubyonra...@googlegroups.com
When they refer to attachments in Paperclip, what they mean is that you are attaching a file to a Rails model instance. The model instance is saved in the database, and the file in the filesystem, so it is an attachment if you look at it the way they mean you to.

Walter

>
> --
> 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/e4269bd0-2129-42c0-bd4b-0e88572a468b%40googlegroups.com.

fugee ohu

unread,
Jan 22, 2016, 6:00:32 PM1/22/16
to Ruby on Rails: Talk
  ty

fugee ohu

unread,
Jan 22, 2016, 9:17:12 PM1/22/16
to Ruby on Rails: Talk
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:

Przemek Kosakowski

unread,
Jan 22, 2016, 9:20:04 PM1/22/16
to rubyonra...@googlegroups.com
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:
--
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.

fugee ohu

unread,
Jan 22, 2016, 9:40:13 PM1/22/16
to Ruby on Rails: Talk
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 ?

fugee ohu

unread,
Jan 22, 2016, 9:42:14 PM1/22/16
to Ruby on Rails: Talk
On Thursday, January 21, 2016 at 11:16:40 PM UTC-5, fugee ohu wrote:

 I'm staying the course with carrierwave because I wanna learn to use it because of what I read here https://infinum.co/the-capsized-eight/articles/best-rails-image-uploader-paperclip-carrierwave-refile
Message has been deleted

Walter Lee Davis

unread,
Jan 23, 2016, 9:58:22 AM1/23/16
to rubyonra...@googlegroups.com

> On Jan 22, 2016, at 9:40 PM, fugee ohu <fuge...@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-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/24627943-9494-4959-8d57-8726a6627122%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages