Rails 4.1用户名和头像不会保存

时间:2014-08-27 15:24:28

标签: ruby-on-rails-4 paperclip avatar

好的......我有一个非常奇怪的问题。创建新用户时,由于某些奇怪的原因,它不会保存用户名或头像...但仅限于他们使用电子邮件注册时。当他们与Facebook完全相同的事情...用户和头像工作正常。这是日志中的粘贴:

Parameters: {"utf8"=>"â", "authenticity_token"=>"QDa9azqwenwcyCWPaYWohDDgSRBST3eCRTcagky984=", "user"=>{"ip_address"=>"77.777.77.777", "name"=>"Jim Beam", "email"=>"jim@example.com", "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”是一个可访问的属性,就像“:avatar”一样,据我所知它们都设置正确,因为它适用于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)工作正常,因为我们可以看到它试图在日志中保存名称和头像。

无论如何,如果你能想出任何东西,请告诉我。

编辑:找出丢失的拇指,但当用户尝试上传图片或保存用户名时仍然无效。它保存了用户ID,电子邮件地址和密码......那么为什么不保存名称呢?

1 个答案:

答案 0 :(得分:0)

最终没那么复杂......我应该知道:

  1. 如果它正在为Facebook登录(omniauth)工作但不能正常用户登录(设计)
  2. ......那么它必定是一个设计问题。
  3. 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
    

    所以我需要的是添加:name,:avatar和:ip_address到设计控制器的允许参数,现在一切正常。