我试图用rails 4编码图像上传,并且我已经安装了Paperclip。如何确保在轨道上编码图像上传时的正确路线?
的错误信息是:
“否路线匹配{:行动=>” photoupload”:控制器=> “用户”, :ID => “4”}缺少必需的键:[ :USER_ID]”
这是我route.rb
文件:
resources :users, path: '' do
patch '/photoupload', to: 'users#photoupload'
end
这是我的控制器代码:
class UsersController < ApplicationController
private
def photoupload
@user = User.find(params[:id])
respond_to do |format|
if @user.update(params.require(:user).permit(:user_id))
format.js { render json: {photo: true} }
else
format.js { render json: @user.errors }
end
end
end
end
这是我的模型:
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { :medium => "200x200>", :thumb => "100x100>" }
validates_attachment_content_type :avatar, :content_type => /^image\/(png|gif|jpeg|jpg)/
end
这是我的用户表明的观点:
<%= form_tag user_photoupload_path, method: :patch, id: 'photoinfo', remote: true, html: { multipart: true } do %>
<div class="photoPreview">
<%= icon('upload', '', class: 'photoUpload') %>
<p id="uploadClick">Click to Upload</p>
</div>
<%= file_field_tag :avatar, accept: 'image/png,image/gif,image/jpeg, image/jpg', id: 'uploadAvatar' %>
<p class="deletePhoto">Delete</p>
<%= submit_tag 'Submit Photo', id: 'submitPhoto' %>
<% end %>
这是我的JavaScript文件:
function circleImageClick() {
$('.deletePhoto').hide();
$('.photoPreview').click(function() {
$(this).attr('disabled', 'true');
$('#uploadAvatar').trigger('click');
});
$("#uploadAvatar").change(function(){
$('.photoPreview').removeAttr('disabled');
readURL(this);
});
}
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.photoPreview').css('background', 'url(' + e.target.result + ')');
$('.photoUpload, #uploadClick').hide();
}
$('.deletePhoto').show();
reader.readAsDataURL(input.files[0]);
}
}
function deletePhoto() {
$('.deletePhoto').click(function() {
$('.deletePhoto').hide();
$('#uploadAvatar').val('');
$('.photoPreview').css('background', '');
$('.photoUpload, #uploadClick').show();
});
}
user_photoupload PATCH /:user_id/photoupload(.:format)
users#photoupload
GET/users#index
POST/users#create
的误差,这是行77,78和83处于光载入行动。
这是该方法的定义:
class UsersController < ApplicationController
private
def photoupload
@user = User.find(params[:user_id])
respond_to do |format|
if @user.update(params.require(:user).permit!
77 format.js { render json: {photo: true} }
78 else
format.js { render json: @user.errors }
end
end
end
83 end
我发了照片的独立模式,这完美的作品。在你的情况下,你没有单独的模型的图片,但用户模型与图片必要的列,如image_content_type,image_file_size等..对吗?我不是路由专家,但它似乎你的form_tag作为一个子模型参考照片:user_photoupload_path(但我不知道,也许有人会指导你..) – Maxence
是否有可能显示所有的路线? (你可以输入一个虚拟地址,例如http:// localhost:3000/users/1/whatever并且它们应该显示出来) – Maxence
它是通过cmd还是routes.rb路由? – JohnnyDevv