Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Paperclip get param data in up...

Replytotopic

Paperclip get param data in update action

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 69
Sign in to rate this post

Hello,

I’m using paperclip to add pictures to a project.
One project can have multiple pictures so I’ve added multiple fields. (that’s working fine)
But when I update a project with empty (picture)fields, paperclip is creating empty entries in the photo model.

So my question is, how can I get the newly added pictures in an array?
I’ve tried params:project but that isn’t returning anything.

Thanks

 
664297762

Authority 50
Posting Rating 95
Sign in to rate this post

Hi Wouter,

You can access new array(picturs) content like:

ex :
Im considering edit form as:

<% form_for( @person, :html => { :multipart => true } do |person_form| %>
   First name: <%= person_form.text_field :first_name %>
   Last name : <%= person_form.text_field :last_name %>
<% fields_for @person.photos do |p| %>
   Enxisting  image: <%= p.file_field :image %>
<% end %>

<% 5.times do |p| %> <= file_field_tag “personnewphotos” %>
<
end >
<
end%>

Now in your controllers :update function will be:

def update
@person = Person.find(params[:id])

if @person .update_attributes(params[:person])
params\[:person]\[:newphotos] ||= []
@personNewPhotos = params\[:person]\[:newphotos]

@personNewPhotos.each do |newPhoto|
@photo = @person.photos.new
@photo.image = newPhoto
@photo.save
end
end
end

I hope this will help you..

Good Luck..!

:) :)

 
Profile

Authority 0
Posting Rating 69
Sign in to rate this post

Hi Ganesh,

Thanks for your help, I’ll show an example of my code (http://pastie.org/727904)

At the end I’ve included update.html.erb for testing. As you can see, I retrieve something, but I’d like to get the real photo data.

Can you help me with this?

 
664297762

Authority 50
Posting Rating 95
Sign in to rate this post

Hi Wouter,

http://www.webyfi.com/?p=11

This article will help you more..!

Good Luck..!

 
Profile

Authority 0
Posting Rating 69
Sign in to rate this post

Thanks Ganesh,

These lines of code solved the problem: def photo_attributes=(photo_attributes)
photo_attributes.each do |attributes|
photos.build(attributes) if !attributes[:image].blank?
end
end

Replytotopic

Other Recent Topics

Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel