设计注册控制器+回形针

时间:2011-04-21 00:27:54

标签: ruby-on-rails devise paperclip crop

我试图覆盖设计注册控制器,以便用户能够上传他的头像以及更改其他数据,并在上传后裁剪用户图片。

我添加了所有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

但是当我提交带有图片的表单时,没有任何反应,除了firefox显示“正在加载......”!绝对没有开发日志中的更新.. :(

谁能告诉我我能做错什么?

PS。用户编辑表单看起来像:

<%= form_for(@user, :url => registration_path(@user), :html => {:id => "userpic_form", :method => :put, :multipart => true}) do |f| %>
 <p class="box1_po">Current password: <%= f.password_field :current_password %></p>
 <p class="box1_po">Please select your user picture:
                                            <%= f.file_field :avatar  %>
 </p>
 <input type="submit" class="usubmit"><%= link_to "UPLOAD", "#", :onclick => "$('#userpic_form').submit();"%>
<% end %>

3 个答案:

答案 0 :(得分:7)

发生我只需要添加

attr_accessible :avatar
在用户模型中

,它开始正常工作

答案 1 :(得分:2)

如果您使用 Rails 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

答案 2 :(得分: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| %>