gpt4 book ai didi

ruby-on-rails - Rails Valums Ajax 上传

转载 作者:行者123 更新时间:2023-12-04 06:19:44 25 4
gpt4 key购买 nike

我试图让 Valums uploader 与我的 Rails 项目一起工作,但遇到了很多困难。

我目前有一个使用标准模型和 View 的 Paperclip 的非常简单的上传过程......型号

class User
include Mongoid::Document
include Mongoid::Paperclip

has_mongoid_attached_file :image

Controller

  def avatar
@user = current_user
respond_to do |format|
format.html
end
end
#working on the updateimage method
def update
file = params[:qqfile].is_a?(ActionDispatch::Http::UploadedFile) ? params[:qqfile] : params[:file]
@user = current_user
if @user.update_attributes(params[:user])
render :text => '{"success": true}', :content_type => "application/json"
else
render :text => @user.errors.to_json, :content_type => "application/json"
end
end

查看

= form_for(@user, :as => @user, :url => '/updateimage', :html => { :method => :post, :multipart => true }) do |f|
#file-uploader
=@user.firstname
%img{:src => current_user.image}
= f.file_field :image
= f.submit

Soooooo... 这一切都有效,但是当我尝试使用 Valums jQuery 时:

  $('#file-uploader').fineUploader({
debug: true,
autoSubmit: true,
allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
sizeLimit: 1048576, // max size: 1MB
minSizeLimit: 0, // min size
multiple: false,
request: {
endpoint: '/updateimage',
paramsInBody: true
}
});

我明白了:

undefined method `update_attributes' for nil:NilClass

我很想让它工作,但我对一般的编程有点陌生,所以它对我来说仍然是一个相当抽象的东西。我很乐意提供额外的日志信息,只需告诉我在哪里可以找到它。

路线

             admin_index GET    /admin(.:format)                       admin#index
POST /admin(.:format) admin#create
new_admin GET /admin/new(.:format) admin#new
edit_admin GET /admin/:id/edit(.:format) admin#edit
admin GET /admin/:id(.:format) admin#show
PUT /admin/:id(.:format) admin#update
DELETE /admin/:id(.:format) admin#destroy
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
edit_entry GET /entries/:id/edit(.:format) entries#edit
entry GET /entries/:id(.:format) entries#show
PUT /entries/:id(.:format) entries#update
DELETE /entries/:id(.:format) entries#destroy
home_index GET /home(.:format) home#index
POST /home(.:format) home#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/:id/edit(.:format) home#edit
home GET /home/:id(.:format) home#show
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
avatar /avatar(.:format) home#avatar
updateimage POST /updateimage(.:format) home#update
root / home#home
root / home#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook)
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user GET /users/:id(.:format) users#show

最佳答案

更新:我认为值得尝试单独使用 users#update 并创建一个单独的文件上传操作。

至于路由,FineUploader 仅执行POST 请求,因此您必须添加一个新路由,因为POST 请求到/users 端点会触发 users#create 操作,这不是您想要的。

match '/avatar', :to => 'users#avatar', :via => :post

此外,您还必须在服务器端处理文件上传。 This wiki page描述了两种方法。例如,您可以使用 rack-raw-upload gem 并将 Controller 代码更改为以下内容:

def update
# this action does not handle AJAX file upload
# ...
end

def avatar
file = params[:qqfile].is_a?(ActionDispatch::Http::UploadedFile) ? params[:qqfile] : params[:file]
@user = current_user
if @user.update_attributes({ :image => file })
render :json => { :success => true }
else
render :json => { :success => false }
end
end

请注意,config/application.rb 中必须有一行(如果您使用 rack-raw-upload gem):

config.middleware.use 'Rack::RawUpload'

另外,paramsInBody好像应该属于request参数:

$('#file-uploader').fineUploader({
request: {
endpoint: "/avatar",
paramsInBody: true
},
debug: true
})

最后的注意事项:发出 AJAX 请求时,CSRF token 必须包含在 header 中,以便启用用户身份验证。这可以通过编辑 fineuploader.js 中的 setHeaders 函数来完成——将以下行添加到该函数中:

xhr.setRequestHeader("X-CSRF-Token", $("meta[name='csrf-token']").attr("content"));

关于ruby-on-rails - Rails Valums Ajax 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15254470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com