2011-04-21 29 views
7

Tôi đang cố gắng ghi đè lên bộ điều khiển đăng ký để người dùng có thể tải hình đại diện của mình cùng với việc thay đổi dữ liệu khác và cắt người dùng sau khi tải lên.tạo bộ điều khiển đăng ký + kẹp giấy

i thêm tất cả thuộc tính người dùng kẹp giấy necesarry, xem cây trồng tạo ra, và điều khiển đăng ký của tôi trông như thế:

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 

nhưng khi tôi gửi biểu mẫu với hình ảnh, không có gì xảy ra, ngoại trừ việc firefox cho thấy "bốc. .." mãi mãi! hoàn toàn không có thông tin cập nhật trong nhật ký phát triển .. :(

bất cứ ai có thể cho tôi biết những gì tôi có thể làm sai

dùng ps chỉnh sửa hình thức trông như thế:?.

Trả lời

7

xảy ra tôi chỉ phải thêm

attr_accessible :avatar 

trong mô hình dùng, và nó bắt đầu hoạt động bình thường

+4

Cách này được thực hiện cho ứng dụng Rails 4? –

2

Nếu bạn sử dụng Rails 4, thêm dòng sau vào các 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. Đảm bảo param được phép trong bộ điều khiển như dưới đây: '

    def configure_permitted_parameters 
         devise_parameter_sanitizer.permit(:account_update, keys: [:firstname, 
            :lastname, :username, :password, :email, :bio, 
            :avatar,:password_confirmation, :current_password ]) 
    end` 
    
  2. Hãy chắc chắn rằng bạn thêm thẻ này : :html => { :multipart => true } vào biểu mẫu của bạn:

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

Các vấn đề liên quan