在轨道中编码图像上传时,如何确保正确的路线?

时间:2016-06-08 23:10:04

标签: javascript ruby-on-rails

我尝试使用rails 4对图像上传进行编码,并且我已经安装了Paperclip。

错误消息是:

  

"没有路线匹配{:action =>" photoupload",:controller =>" users",   :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行中的错误在photoupload动作中。

这是方法定义:

   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                                                                 

2 个答案:

答案 0 :(得分:0)

更新

params.require(:user).permit(:user_id)

params.require(:user).permit(:id)

答案 1 :(得分:0)

从以下位置更新routes

resources :users, path: '' do
  patch '/photoupload', to: 'users#photoupload'
end

为:

resources :users, path: '' do
  patch '/photoupload', to: 'users#photoupload', on: :member
end

并更新

params.require(:user).permit(:user_id)

为:

params.require(:user).permit!