2014-01-29 28 views
0

我正在將我的Rails應用程序從3.2升級到4,除了新用戶使用Devise註冊外,我的工作進展順利。我正在升級到Devise 3.2.2。當我嘗試註冊一個新用戶時,出現用戶圖像的錯誤,「沒有選擇的文件不能變爲空白」。我不認爲這是Paperclip的問題,因爲與我的其他模型上傳的圖像工作正常。我看到它說有不允許的參數,這可能是一個問題的標誌。下面是用戶模型的部分:升級到設計3.2.2 - 圖片上傳給出錯誤「沒有選擇文件」

has_attached_file :image, styles: { small: "75x108>"} 
    validates_attachment :image, presence: true, 
    content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/gif', 'image/png'] }, 
    size: { less_than: 5.megabytes } 

這裏是我的註冊頁面:

<div class="panel panel-default form_page"> 
<div class="panel-heading"> 
    <h2>Sign up</h2> 
</div> 
<div class="panel-body input_container"> 
    <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 
    <%= f.error_notification %> 
    <div class="form-group"> 
     <%= f.input :image, label: "Upload A Profile Pic" %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :name, input_html: {:class => "form-control" } %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :email, input_html: {:class => "form-control" } %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :password, input_html: {:class => "form-control" } %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :password_confirmation, input_html: {:class => "form-control" } %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :twitter, label: "Enter your Twitter handle", input_html: {:class => "form-control" } %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :lastfm, label: "Enter your last.fm handle", input_html: {:class => "form-control" } %> 
    </div> 
    <div class="form-group"> 
     <%= f.input :status, label: "Leave some words of wisdom", input_html: {:class => "form-control" } %> 
    </div> 

    <%= f.submit "Sign up", class: "btn btn-primary" %> 

<% end %> 
</div> 
<div class="panel-footer"> 
    <%= render "devise/shared/links" %> 
</div> 

這裏是我的服務器輸出:

Processing by Devise::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"akj/sZRPil4h3tu9qntjmbgucai2h80/b8U3zhYtEoQ=", "user"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000106bd3dd0 @tempfile=#<Tempfile:/var/folders/r8/z7hzv9r51nv9vymbj1szxqdw0000gp/T/RackMultipart20140129-36058-dwch5z>, @original_filename="tony.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[image]\"; filename=\"tony.jpg\"\r\nContent-Type: image/jpeg\r\n">, "name"=>"Tony", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "twitter"=>"@Tambe257", "lastfm"=>"Tambe257", "status"=>"words"}, "commit"=>"Sign up"} 
Unpermitted parameters: image, name, twitter, lastfm, status 
    (0.1ms) begin transaction 
    User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    (0.1ms) rollback transaction 
+0

您對強參數的預感是正確的。閱讀此信息以獲取有關強參數的更多信息,並設計https://github.com/plataformatec/devise#strong-parameters – Dhaulagiri

+0

@Dhaulagiri謝謝!我將所有u.permit參數添加到控制器。如果你想把它作爲答案,我會檢查它解決問題。 –

回答

0

您對預感強大的參數是正確的。閱讀this瞭解更多有關強參數和設計的信息,它應該可以幫助您解決問題。就像這樣,其中:用戶名是您要添加的字段。

class ApplicationController < ActionController::Base 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) << :username 
    end 
end 
相關問題