2

Rails 4,设计 3.0.3,Oauth-facebook

我在我的用户模型中添加了两个额外的参数 - :name、:uid 并尝试从我的表单(路由 /users/sign_up)将它保存在我的用户表中。但结果我收到了表中的记录,它只包含字段的默认值:名称和:uid,而不是值,我把它放在我的 text_fields 中。

在控制台中,我收到以下消息:

Unpermitted parameters: name, uid
WARNING: Can't mass-assign protected attributes for User: password_confirmation
app/models/user.rb:31:in `new_with_session'

这是我的 user.rb 模型。我试图从 attr_accessible 中删除这些字段,但它没有给出任何结果。

class User < ActiveRecord::Base
  attr_accessible :oauth_expires_at, :oauth_token, :oauth_secret, :email, :password, :uid, :provider, :name

  default_scope -> {order('created_at ASC')}

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, 
     :omniauthable, :omniauth_providers => [:facebook]

  has_many :microposts, :primary_key => "uid", dependent: :destroy

  # validates :uid, presence: true

  def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  user = User.where(:provider => auth.provider, :uid => auth.uid).first
  unless user
    user = User.create(name:auth.extra.raw_info.name,
                     provider:auth.provider,
                     uid:auth.uid,
                     email:auth.info.email,
                     password:Devise.friendly_token[0,20]
                     )
  end
  user
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"]
    user.email = data["email"]
      end
    end
  end

end

这是我的 users/omniauth_callbacks_controller.rb (没有 facebook 方法)。我尝试应用与 before_filter 相关的不同建议,但它仍然不起作用

 class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

 before_filter :configure_permitted_parameters

 def create
    super
 end

 private

 def configure_permitted_parameters
   params.require(:user).permit(:name, :uid, :provider)
 end

 end

这是我的视图形式()

注册

    <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>

    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>

      <div><%= f.label :name %> <br /> 
      <%= f.text_field :name, :autofocus => true %></div>

      <div><%= f.label :email %><br />
      <%= f.email_field :email %></div>

      <div><%= f.label :password %><br />
      <%= f.password_field :password %></div>

      <div><%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation %></div>

      <div><%= f.label :uid %><br />
      <%= f.text_field :uid %></div>


      <div><%= f.submit "Sign up" %></div>
    <% end %>

    <%= render "devise/shared/links" %>

你能帮我吗,我不明白我在做什么错。如何为我的强参数配置白名单以接收正确的值(哪个用户放在视图表单中)?

我所有的源代码都可以在这里找到:https ://github.com/DavydenkovM/d23m

提前致谢!

更新

我已经删除了 attr_accessible 字段并更新了我的控制器。但是在同一点上存在未经许可的参数名称和 uid 的问题。现在我的控制器看起来像:

    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

      before_filter :configure_permitted_parameters, if: :devise_controller?

      def facebook

        @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

        if @user.persisted?
          sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
          flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
          set_flash_message(:notice, :success, :kind => "Facebook") # if is_navigational_format?
        else
          redirect_to root_url if user_signed_in?
          session["devise.facebook_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end

      def create
        super
      end

      #def update
      #  person = current_account.user.find(params[:id])
      #  person.update_attributes!(person_params)s
      #  redirect_to person
      #end

      private

      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:users) do |u|
          u.permit(:name, :email, :password, :password_confirmation, :uid, :provider)
        end
      end
    end

更新 2. 我不清楚 devise_parameter_sanitizer.for(?) 中的资源是什么以及我需要在哪里分配它?

4

2 回答 2

1

请尝试以下方法。

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  user = User.where(:provider => auth.provider, :uid => auth.uid).first
  unless user
    user = User.create(name:auth.extra.raw_info.name,
                     provider:auth.provider,
                     uid:auth.uid,
                     email:auth.info.email,
                     password:Devise.friendly_token[0,20]
                     ).permit!(:name, :uid, :provider)
  end
  user
end

或者

def facebook

 @user = User.find_for_facebook_oauth(request.env["omniauth.auth"].permit!(:name, :uid, :provider), current_user)

   if @user.persisted?
   sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
    flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
    set_flash_message(:notice, :success, :kind => "Facebook") # if is_navigational_format?
  else
    redirect_to root_url if user_signed_in?
    session["devise.facebook_data"] = request.env["omniauth.auth"]
    redirect_to new_user_registration_url
  end
end

然后试试这个

def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password, :password_confirmation, :uid, :provider) }
end
于 2013-09-05T08:37:54.210 回答
0

attr_accessible在 Rails 4 中不可用,因为现在默认使用强参数。看来您已经在使用强参数,因此您应该简单地从用户模型attr_accessible中删除该行。

于 2013-09-05T06:34:42.353 回答