Rails 3.1 +回形针 + jQuery 文件上传

发布于 2024-12-12 00:40:23 字数 803 浏览 2 评论 0原文

我一直在寻找一种使用 PaperclipjQuery 文件上传。查看 jQuery fileupload 页面上的教程,我设置了系统但我找不到一种方法让回形针处理上传的文件。

这就是我所拥有的(简而言之):

# model
has_attached_file :photo ...

# view
= form_for @user, :html => {:multipart => true} do |f|
  f.file_field :photo, :multiple => true
  f.submit

# controller
def create
  @user = User.new params[:user]
  if @user.save
    render :json => [...]
end

如果我检查提交的数据,我会在 params[:user] 和 params[:user][:photo] 中获取所有用户属性,即 ActionDispatch::Http::UploadedFile。当调用 @user.save 时,图像不会被处理或保存。

线索、教程或解决方案将不胜感激!

I've been looking for a way to set up Ruby on Rails 3.1 with Paperclip and jQuery fileupload. Looking at a tutorial at jQuery fileupload page I got the system set up but I can't find a way to make paperclip process the uploaded file.

Here's what I have (in short):

# model
has_attached_file :photo ...

# view
= form_for @user, :html => {:multipart => true} do |f|
  f.file_field :photo, :multiple => true
  f.submit

# controller
def create
  @user = User.new params[:user]
  if @user.save
    render :json => [...]
end

If I inspect submitted data I get all user properties in params[:user] and params[:user][:photo] which is ActionDispatch::Http::UploadedFile. When @user.save is called the image doesn't get processed or saved.

A clue, tutorial or a solution would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福%小乖 2024-12-19 00:40:23

在解决这个问题一段时间后,我发现问题出现在 :multipart =>; 时。 true 被添加到 f.file_field,因为表单字段名称从 user[photo] 更改为 user[photo][] >。

使用单独的页面附加照片

我希望有一个单独的页面用于将多个文件上传到记录(以及另一个用于编辑用户属性的页面)。在我看来,这只是一个临时解决方案,但它确实有效。而不是 f.form_field :photo, :multipart => true 我使用了 form_field_tag 'user[photo]', :multiple =>; true 在视图中。

我的代码如下所示:

## app/model/user.rb
has_attached_file :photo

def to_fileupload_json
  {
    "name" => photo_file_name,
    "size" => photo_file_size,
    ...
  }
end

## app/views/photos/new.html.haml
= form_for @user, :url => users_path, :html => { :multipart => true } do |f|
  #fileupload
    = file_field_tag 'user[photo]', :multiple => true
    = f.submit

= javascript_include_tag "jquery.fileupload.js"
# require all other JS files needed for the plugin (jQuery, jQuery UI, ...)
= stylesheet_link_tag "jquery.fileupload-ui.css"
= render :partial => "jquery_file_templates" # partial with jQuery templates for responses 

:javascript
  $(function () {
      uploader = $('#fileupload').fileupload()
  }


## app/controllers/users_controller.rb
def create
  @user = User.create params[:user]
end

如果其他人知道更好的方法(正确的方法),请告诉我们!

使用 fields_for

根据应用的结构,您可能会考虑使用 fields_for

在这种情况下,您必须:

  • accepts_nested_attributes_for :photos 添加到您的(用户)模型
  • 将方法 photos_attribues=(attributes) 添加到您的(用户)模型并处理记录 在用户的新方法中创建
  • 照片 3.times { @user.photos.build }

示例:

def photos_attribues=(attributes)
  attributes.each do |key, value|
    photo = Photo.create :photo => value, ...
  end
end

免责声明:上面的代码已被简化/重写使其更容易理解。我可能在删除不需要的东西时犯了错误。再说一遍 - 我不太确定这是解决这个问题的正确方法。非常欢迎提出建议和改进!

After struggling with this problem for a while I discovered the problem appeared when :multipart => true is added to the f.file_field since the form field name changes from user[photo] to user[photo][].

Using separate page to attach photos

I want to have a separate page for uploading multiple files to a record (and another one for editing User's properties). This looks to me as a temporary solution but it works. Instead of f.form_field :photo, :multipart => true I used form_field_tag 'user[photo]', :multiple => true in the view.

My code looks like this:

## app/model/user.rb
has_attached_file :photo

def to_fileupload_json
  {
    "name" => photo_file_name,
    "size" => photo_file_size,
    ...
  }
end

## app/views/photos/new.html.haml
= form_for @user, :url => users_path, :html => { :multipart => true } do |f|
  #fileupload
    = file_field_tag 'user[photo]', :multiple => true
    = f.submit

= javascript_include_tag "jquery.fileupload.js"
# require all other JS files needed for the plugin (jQuery, jQuery UI, ...)
= stylesheet_link_tag "jquery.fileupload-ui.css"
= render :partial => "jquery_file_templates" # partial with jQuery templates for responses 

:javascript
  $(function () {
      uploader = $('#fileupload').fileupload()
  }


## app/controllers/users_controller.rb
def create
  @user = User.create params[:user]
end

If anyone else knows a better way (the right way) of doing this, please let us know!

Using fields_for

Depending on the structure of your app you might consider using fields_for.

In this case you'll have to:

  • add accepts_nested_attributes_for :photos to your (User) model
  • add a method photos_attribues=(attributes) to your (User) model and handle record creation there
  • build record for photos 3.times { @user.photos.build } in User's new method

Example:

def photos_attribues=(attributes)
  attributes.each do |key, value|
    photo = Photo.create :photo => value, ...
  end
end

Disclaimer: The code above was simplified/rewritten to make it easier to understand. I might've made mistakes while erasing unneeded stuff. And again - I'm not really sure this is the right way of solving this problem. Suggestions and improvements are more than welcome!

横笛休吹塞上声 2024-12-19 00:40:23

如果您在 file_field 上显式设置名称:

f.file_field :photo, multiple: true, name: 'user[photo]'

Rails 将生成 user[photo] 而不是 user[photo][]

If you explicitly set the name on the file_field:

f.file_field :photo, multiple: true, name: 'user[photo]'

Rails will generate user[photo] instead of user[photo][].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文