form_for @object with carrierwave upload form

This form raises undefined method pictures_path’ for #<#Class:0xaea3ad0:0xbb9cb4c>`

<%= form_for(@picture, :html => { :multipart => true }) do |f| %>

<%= f.file_field :image %>

<%= f.submit %>

<% end %>

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

I see you trying upload some picture, please use
this gem [1]. You be able in easy way customize your picture uploader.

  [1]: [paperclip](https://github.com/thoughtbot/paperclip)

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

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

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

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

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.

Hi,

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

many pictures you mean “a few different user
profile photo” or you mean “few copy of the same picture but
different size/dimension” ?

Many pictures, not just different versions of the same picture

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 o f Picture. Even on fb user be
able to have only one profile picture.

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

#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

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

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.”
else
render “new”
end
end

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

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 %>

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

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

ty

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