2011-04-21 67 views
7

我想重寫設計註冊控制器,以便用戶將能夠上傳他的頭像以及更改其他數據,並在上傳後裁剪userpic。設計註冊控制器+回形針

我添加了所有necesarry用戶回形針屬性,創建作物觀點,我的註冊控制器看起來像這樣:

class RegistrationsController < Devise::RegistrationsController 
    def update 

    if params[resource_name][:avatar].blank? 
      super 
    else 
      @user=resource 
     respond_to do |format| 
     if resource.update_attributes(params[resource_name]) 
      flash[:notice]='Avatar successfully uploaded.' 
      format.html { 
        render :action => 'crop' 
      } 
      format.xml { head :ok } 
      else 
      format.html { render :action => "editpicture" } 
      format.xml { render :xml => @demotivator.errors, :status => :unprocessable_entity } 
      end 
     end 
    end 
    end 

end 

,但是當我用圖片提交表單,什麼也沒有發生,除了火狐顯示「加載。 ..「永遠!在開發日誌中絕對沒有更新.. :(

任何人都可以告訴我,我該怎麼辦是做錯了

PS用戶編輯表格看起來像這樣:?

回答

7

時我不得不添加

attr_accessible :avatar 

在用戶模式,並開始如果你是正常

+4

雖然Rails 4應用程序如何完成這項工作? – 2013-08-17 00:15:52

2

使用軌道4,添加以下到RegistrationsController

# get devise to recognize the custom fields of the user model 
before_filter :configure_permitted_parameters, if: :devise_controller? 

protected 

    def configure_permitted_parameters 
     devise_parameter_sanitizer.for(:account_update) do |u| 
      u.permit(:avatar, :email, :password, :password_confirmation) 
     end 
    end 
0
  1. 確保參數是允許在控制器象下面這樣: `

    def configure_permitted_parameters 
         devise_parameter_sanitizer.permit(:account_update, keys: [:firstname, 
            :lastname, :username, :password, :email, :bio, 
            :avatar,:password_confirmation, :current_password ]) 
    end` 
    
  2. 確保您添加此標籤::html => { :multipart => true }發送到您的表單:

    <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }, :html => { :multipart => true }) do |f| %>