Bo Zhao
2012-Jan-30 06:51 UTC
how to use Paperclip alone with a separated attachment model
I have a scenario that my user need to upload a image first and then assign to another model, so I used a generic model, to upload the file first. But I always get a exception of ActiveRecord::UnknownAttributeError (unknown attribute: utf8). because rails automatically adds a utf-8 field for the form submit. model: <code> class AttachedImage < ActiveRecord::Base belongs_to :attachable, :polymorphic => true has_attached_file :image end </code> migration: <code> class CreateAttachedImages < ActiveRecord::Migration def change create_table :attached_images do |t| t.string :image_file_name t.string :image_content_type t.integer :image_file_size t.datetime :image_updated_at t.references :attachable, :polymorphic => true t.timestamps end end end </code> And the controller: <code> class AttachedImagesController < ApplicationController def create @attched_image = AttachedImage.new(params) respond_to do |format| if @attched_image.save format.js else format.js end end end end </code> the view part: <code> <div id="upload-image-dialog"> <%= form_tag(@attached_image, :method => "POST", :remote => true, :html => { :multipart => true }) do %> <%= file_field_tag :image %> <%= submit_tag("submit") %> <% end %> <h1 style="display:none">Successfully Uploaded.</h1> </div> </code> it''s quite straight forward, but every time I submit this form, I will get an exception of the utf8 field added automatically by rails. ActiveRecord::UnknownAttributeError (unknown attribute: utf8): app/controllers/attached_images_controller.rb:5:in `new'' app/controllers/attached_images_controller.rb:5:in `create'' I could not understand that, this shall not be a problem right? We are writing @model = Model.new(params) everyday, hope someone could help me out and explain what''s going on under the hood, thanks! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Prince Joseph
2012-Jan-30 07:43 UTC
Re: how to use Paperclip alone with a separated attachment model
Problem is here, in this line of code: @attched_image = AttachedImage.new(params) It should be: @attched_image = AttachedImage.new(params[:attached_image]) On Mon, Jan 30, 2012 at 12:21 PM, Bo Zhao <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have a scenario that my user need to upload a image first and then > assign to another model, so I used a generic model, to upload the file > first. But I always get a exception of > ActiveRecord::UnknownAttributeError (unknown attribute: utf8). because > rails automatically adds a utf-8 field for the form submit. > > model: > <code> > class AttachedImage < ActiveRecord::Base > belongs_to :attachable, :polymorphic => true > > has_attached_file :image > end > </code> > > migration: > <code> > class CreateAttachedImages < ActiveRecord::Migration > def change > create_table :attached_images do |t| > > t.string :image_file_name > t.string :image_content_type > t.integer :image_file_size > t.datetime :image_updated_at > t.references :attachable, :polymorphic => true > > t.timestamps > end > end > end > </code> > > And the controller: > <code> > class AttachedImagesController < ApplicationController > def create > > @attched_image = AttachedImage.new(params) > > respond_to do |format| > if @attched_image.save > format.js > else > format.js > end > end > end > end > </code> > > the view part: > <code> > <div id="upload-image-dialog"> > <%= form_tag(@attached_image, :method => "POST", :remote => true, > :html => { :multipart => true }) do %> > <%= file_field_tag :image %> > <%= submit_tag("submit") %> > <% end %> > <h1 style="display:none">Successfully Uploaded.</h1> > </div> > </code> > > it''s quite straight forward, but every time I submit this form, I will > get an exception of the utf8 field added automatically by rails. > > ActiveRecord::UnknownAttributeError (unknown attribute: utf8): > app/controllers/attached_images_controller.rb:5:in `new'' > app/controllers/attached_images_controller.rb:5:in `create'' > > I could not understand that, this shall not be a problem right? We are > writing @model = Model.new(params) everyday, hope someone could help me > out and explain what''s going on under the hood, thanks! > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Thanks, Prince -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
subbarao
2012-Jan-30 13:26 UTC
Re: how to use Paperclip alone with a separated attachment model
On Monday 30 January 2012 12:21 PM, Bo Zhao wrote:> I have a scenario that my user need to upload a image first and then > assign to another model, so I used a generic model, to upload the file > first. But I always get a exception of > ActiveRecord::UnknownAttributeError (unknown attribute: utf8). because > rails automatically adds a utf-8 field for the form submit. > > model: > <code> > class AttachedImage< ActiveRecord::Base > belongs_to :attachable, :polymorphic => true > > has_attached_file :image > end > </code> > > migration: > <code> > class CreateAttachedImages< ActiveRecord::Migration > def change > create_table :attached_images do |t| > > t.string :image_file_name > t.string :image_content_type > t.integer :image_file_size > t.datetime :image_updated_at > t.references :attachable, :polymorphic => true > > t.timestamps > end > end > end > </code> > > And the controller: > <code> > class AttachedImagesController< ApplicationController > def create > > @attched_image = AttachedImage.new(params) > > respond_to do |format| > if @attched_image.save > format.js > else > format.js > end > end > end > end > </code> > > the view part: > <code> > <div id="upload-image-dialog"> > <%= form_tag(@attached_image, :method => "POST", :remote => true, > :html => { :multipart => true }) do %> > <%= file_field_tag :image %> > <%= submit_tag("submit") %> > <% end %> > <h1 style="display:none">Successfully Uploaded.</h1> > </div> > </code> > > it''s quite straight forward, but every time I submit this form, I will > get an exception of the utf8 field added automatically by rails. > > ActiveRecord::UnknownAttributeError (unknown attribute: utf8): > app/controllers/attached_images_controller.rb:5:in `new'' > app/controllers/attached_images_controller.rb:5:in `create'' > > I could not understand that, this shall not be a problem right? We are > writing @model = Model.new(params) everyday, hope someone could help me > out and explain what''s going on under the hood, thanks! >change in the controller @attched_image = AttachedImage.new(params) to @attched_image = AttachedImage.new(params[:attached_image]) if above not working because of form_tag usage then you have to use custom new like @attched_image = AttachedImage.new(:image=>params[:image]) this should work. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.