Forum: Ruby on Rails upload_column problems on post after validation error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
110e527af05961700d9e39285d043b62?d=identicon&s=25 Gianluca Tessarolo (Guest)
on 2007-05-28 22:37
(Received via mailing list)
Hi to all the crew !

I've a little (big ?) problem.

I'm using upload_column plugin, it works very well but it throws an
exception when I try to repost the form after a validation error.

Here some details:

Model: (hostel.rb)

  HOSTEL_PHOTO_DIR = Proc.new {|inst, attr| "hostel/#{inst.id}"}
  HOSTEL_PHOTO_THUMB = "100x100"
  HOSTEL_PHOTO_NORMAL = "400x400"

  image_column :photo_1, :store_dir => HOSTEL_PHOTO_DIR,
                         :versions => { :thumb => HOSTEL_PHOTO_THUMB,
:normal => HOSTEL_PHOTO_NORMAL},
                         :filename => Proc.new {|inst, orig, ext|
"photo_1.#{ext}"}

View (signup_customer.rhtml):
  <% upload_form_tag :action => "update_customer" do %>
    ...
    <label for="hostel_photo_1">Photo 1</label>
    <%= upload_column_field "hostel", "photo_1" %>
  <% end %>

Controller:
  def update_customer
    begin
      ...
      hostel.attributes = params[:hostel]   # Where the error
occurred...
      hostel.save!
    rescue
      @hostel = hostel
       render :action => "signup_customer"
    end
  end

Now, if I post the form without validation exception, all works good !!!

But if I try to post the form without a required field I give an
exception and the controller show the form again, if I put the required
data and post again I receive this exception:

invalid format of 'hostel/117/photo_1.jpg;0182-Problemi-gravi.jpg'

|vendor/plugins/uploadcolumn/lib/upload_column.rb:431:in `set_path'
vendor/plugins/uploadcolumn/lib/upload_column.rb:809:in `send'
vendor/plugins/uploadcolumn/lib/upload_column.rb:809:in `photo_1_temp='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
`send'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
`attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
`each'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
`attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/association_proxy.rb:123:in
`send'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/association_proxy.rb:123:in
`method_missing'
app/controllers/hostel_controller.rb:53:in `update_customer'|

At line 53 in hostel_controller.update_customer there is the line:

      hostel.attributes = params[:hostel]   # Where the error
occurred...

I don't know how to solve this problem !!!

Can you help me ????

Thanks in advance !!!

Tex.
A6fa2beee62b866a1672af8b162d3358?d=identicon&s=25 Igor Gladkoborodov (varezhka)
on 2007-09-27 11:25
That's because it tryes to assign photo_1_temp before photo_1.

This should work:
def update_customer
    begin
      ...
      hostel.attributes = params[:hostel][:photo_1] if
params[:hostel][:photo_1]
      hostel.attributes = params[:hostel]   # Where the error occurred
      ...
      hostel.save!
    rescue
      @hostel = hostel
       render :action => "signup_customer"
    end
end
This topic is locked and can not be replied to.