簡體   English   中英

設計注冊控制器+回形針

[英]devise registrations controller + paperclip

我試圖覆蓋設計注冊控制器,以便用戶能夠上傳他的化身以及更改其他數據,並在上傳后裁剪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

但是當我提交帶有圖片的表單時,沒有任何反應,除了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 %>

發生我只需添加

attr_accessible :avatar

在用戶模型中,它開始正常工作

如果您正在使用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
  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| %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM