2016-06-08 75 views
1

我試圖用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                 
+0

我發了照片的獨立模式,這完美的作品。在你的情況下,你沒有單獨的模型的圖片,但用戶模型與圖片必要的列,如image_content_type,image_file_size等..對嗎?我不是路由專家,但它似乎你的form_tag作爲一個子模型參考照片:user_photoupload_path(但我不知道,也許有人會指導你..) – Maxence

+0

是否有可能顯示所有的路線? (你可以輸入一個虛擬地址,例如http:// localhost:3000/users/1/whatever並且它們應該顯示出來) – Maxence

+0

它是通過cmd還是routes.rb路由? – JohnnyDevv

回答

0

更新它

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

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

謝謝,但沒有工作的人 – JohnnyDevv

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! 
+0

做到了這一點,但無濟於事 – JohnnyDevv

+0

你是否得到相同的錯誤?請包括完整的堆棧跟蹤。 – Dharam

+0

C:/loccaff/app/controllers/users_controller.rb:77:語法錯誤,意外的tIDENTIFIER,期待')'format.js {render json:{photo:true}}^C:/ loccaff/app/controllers/users_controller .rb:78:語法錯誤,意外的keyword_else,期待keyword_end else^C:/loccaff/app/controllers/users_controller.rb:83:語法錯誤,意外的關鍵字結束,期待結束輸入結束\t^ – JohnnyDevv

相關問題