2011-04-21 74 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| %>