2014-08-27 21 views
0

好的...我有一個非常奇怪的問題。創建新用戶時,出於某種奇怪的原因,它不會保存用戶名或頭像...但只有當他們使用電子郵件註冊時。當他們做與Facebook完全相同的事情...用戶和頭像工作正常。下面是從日誌中貼:Rails 4.1用戶名和頭像不會保存

Parameters: {"utf8"=>"â", "authenticity_token"=>"QDa9azqwenwcyCWPaYWohDDgSRBST3eCRTcagky984=", "user"=>{"ip_address"=>"77.777.77.777", "name"=>"Jim Beam", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00000008d7a048 @tempfile=#<Tempfile:/tmp/RackMultipart20140827-9443-132d9ai>, @original_filename="joker bench.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"joker bench.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Sign up"} 
Redirected to http://example.com/ 
Completed 302 Found in 599ms (ActiveRecord: 216.3ms) 
Started GET "/" for 77.185.98.140 at 2014-08-27 14:51:31 +0000 
Processing by DailiesController#index as HTML 
    Rendered dailies/index.html.erb within layouts/application (21.2ms) 
Completed 200 OK in 131ms (Views: 110.7ms | ActiveRecord: 19.6ms) 
Started GET "/images/thumb/missing.png" for 77.185.98.140 at 2014-08-27 14:51:31 +0000 

ActionController::RoutingError (No route matches [GET] "/images/thumb/missing.png"): 

正如你所看到的名字和形象正在接收就好了......但後來他們沒有保存到數據庫中。他們只是不在那裏。所以這聽起來像是模型中的問題吧?

class User < ActiveRecord::Base 
    has_merit 

    acts_as_voter 
    acts_as_messageable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    attr_accessible :email, :password, :password_confirmation, :provider, :uid, :name, :remember_me, :avatar, :ip_address, :latitude, :longitude, :avatar, :avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at, :country, :current_sign_in_ip, :id 
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage/ 
def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| 
    user.provider = auth.provider 
    user.uid = auth.uid 
    user.ip_address = user.current_sign_in_ip 
    user.email = auth.info.email || "#{auth.uid}@facebook.com" 
    user.password = user.password_confirmation || SecureRandom.urlsafe_base64(n=6) 
    if auth.provider == "facebook" 
     user.name = auth.info.name  
     if auth.info.image.present? 
     avatar_url = process_uri(auth.info.image) 
     user.update_attribute(:avatar, URI.parse(avatar_url)) 
     end   
    else  
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
    end 
    user.save 
    end 
end 

正如你可以看到「:name」是可訪問的屬性原樣「:阿凡達」而據我所知,他們是正確的設置都因爲它工作得很好,爲Facebook。那麼,爲什麼不爲電子郵件用戶保存它們呢?也許控制器是對的?

這裏的控制器:

class UsersController < ApplicationController 
    before_filter :authenticate_user! 
    before_filter :correct_user?, :except => [:index, :show] 
    before_filter :ip_address 


    def ip_address 
# @user.ip_address = request.remote_ip 
    end 

    def index 
    @users = User.all 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def update 
    @user = User.find(params[:id]) 
    @user.ip_address = request.remote_ip 
    if @user.update_attributes(params[:user]) 
     redirect_to @user 
    else 
     render :edit 
    end 
    end 


    def show 
    @user = User.find(params[:id]) 
    correct_user = @user 
    end 

end 

沒有得到它......不知道這個問題可能是什麼。我可能會在某人回答的時候發現它,但我至少想問。我們知道表單(我正在使用Devise)工作正常,因爲我們可以看到它試圖在日誌中保存名稱和頭像。

無論如何,讓我知道你是否可以拿出任何東西。

編輯:找出丟失的拇指的事情,但當用戶嘗試上傳圖片或保存用戶名時它仍然不起作用。它保存了用戶名,電子郵件地址和密碼...爲什麼它不保存名字呢?

回答

0

不那麼到底複雜......我應該已經知道:

  1. 如果它的工作對Facebook登錄(omniauth),但不工作的普通用戶登錄(設計)
  2. ..那麼它肯定是一個設計問題。

application_controller.rb

before_action :configure_devise_permitted_parameters, if: :devise_controller? 

protected 

    def configure_devise_permitted_parameters 
    registration_params = [:name, :ip_address, :avatar, :email, :password, :password_confirmation] 

    if params[:action] == 'update' 
     devise_parameter_sanitizer.for(:account_update) { 
     |u| u.permit(registration_params << :current_password) 
     } 
    elsif params[:action] == 'create' 
     devise_parameter_sanitizer.for(:sign_up) { 
     |u| u.permit(registration_params) 
     } 
    end 
    end 


end 

因此,所有我需要的是Add:名稱,:頭像,並組成:ip_address爲色器件控制器允許的參數,現在一切工作正常。