开发者

Rails + SWFupload =

开发者 https://www.devze.com 2023-04-11 15:16 出处:网络
my model: (use mysql and i have fields: id, name, image, remote_image_url, created_at, updated_at) require \'mime/types\'

my model: (use mysql and i have fields: id, name, image, remote_image_url, created_at, updated_at)

require 'mime/types'
class Avatar < ActiveRecord::Base            

 has_attached_file :image, :styles => { :small => "150x150>", :large=> "320x240>" }

def swfupload_file=(data)
    data.content_type = MIME::Types.type_for(data.original_filename)
    self.image = data
  end

end

Controller

  def upload
    if params[:Filedata]
      @avatar = Avatar.new(:swf_uploaded_data => params[:Filedata])
      if @avatar.save
        render :partial => 'photo', :object => @avatar
      else
        render :text => "error"
      end
    else
      @avatar = Avatar.new params[:image]
      if @avatar.save
        flash[:notice] = 'Your photo has been uploaded!'
        redirect_to photos_path
      else
        render :action => :new
      end
    end
  end

Index.html.erb

<head>
  <title>.test.</title>
  <%=swfupload_head %>
</head>
<br/><br/>
<%=swfupload :controller=>'test',:action=>'upload'%>

===================================

And I have

Processing TestController#upload (for 127.0.0.1 at 2011-10-07 11:46:29) [POST]
  Parameters: {"request_forgery_protection_token"=>"T2KBI+Wk16fhYHSxUh5HRH72LTIdK//327yuuRkWrqs=", "Filename"=>"bg-body2.jpg", "_test_for_projects_session"=>"BAh7BzoPc2Vzc2lvbl9pZCIlNmU3MGQxMzYzOWI5ZWNmYTk5YzQ4YTU3MWM2YzM3ZDk6EF9jc3JmX3Rva2VuIjFUMktCSStXazE2ZmhZSFN4VWg1SFJINzJMVElkSy8vMzI3eXV1UmtXcnFzPQ==--1084bd5f8d5dd4378a3a40a4e35559875881b6fd", "amp"=>nil, "authenticity_token"=>"T2KBI+Wk16fhYHSxUh5HRH72LTIdK//327yuuRkWrqs=", "Upload"=>"Submit Query", "Filedata"=>#<File:/tmp/RackMultipart20111007-7833-1ryc6s-0>}
  SQL (1.9ms)   SHOW TABLES
  Avatar Columns (1.0ms)   SHOW FIELDS FROM `avatars`
  SQL (1.7ms)   SHOW TABLES
  Avatar Columns (1.0ms)   SHOW FIELDS FROM `avatars`

NoMethodError (undefined method `has_attached_file' for #<Class:0x7ffdca03a7a8>):
  app/models/avatar.rb:35
  app/controllers/test_controller.rb:6:in `upload'

Rendered rescues/_trace (78.6ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)

======

Question: Why is NoMethodError? And why data is not insering into database?

UPD:

I delete in model attached_file... and now data inserts but.. in this view:

Processing TestController#upload (for 127.0.0.1 at 2011-10-07 12:12:31) [POST]
  Parameters: {"request_forgery_protection_token"=>"T2KBI+Wk16fhYHSxUh5HRH72LTIdK//327yuuRkWrqs=", "Filename"=>"Firefox_wallpaper.png", "_test_for_projects_session"=>"BAh7BzoPc2Vzc2lvbl9pZCIlNmU3MGQxMzYzOWI5ZWNmYTk5YzQ4YTU3MWM2YzM3ZDk6EF9jc3JmX3Rva2VuIjFUMktCSStXazE2ZmhZSFN4VWg1SFJINzJMVElkSy8vMzI3eXV1UmtXcnFzPQ==--1084bd5f8d5dd4378a3a40a4e35559875881b6fd", "amp"=>nil, "authenticity_token"=>"T2KBI+Wk16fhYHSxUh5HRH72LTIdK//327yuuRkWrqs=", "Upload"=>"Submit Query", "Filedata"=>#<File:/tmp/RackMultipart20111007-7833-1jylds4-0>}
  Avatar Columns (1.1ms)   SHOW FIELDS FROM `avatars`
  SQL (0.6ms)   BEGIN
  Avatar Create (0.8ms)   INSERT INTO `avatars` (`name`, `remote_image_url`, `created_at`, `updated_at`, `image`) VALUES('', 开发者_StackOverflow中文版'', '2011-10-07 09:12:31', '2011-10-07 09:12:31', '--- !ruby/object:File {}\n\n')


Here is the plugin: https://github.com/jeroeningen/swfupload_45north

has_attached_file is the method that brings attachment_fu to life.

The gem assumes you know about attachment_fu. It is not part of the plugin. You have to do that part yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号