首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails: ActiveModel::ForbiddenAttributesError

Rails: ActiveModel::ForbiddenAttributesError
EN

Stack Overflow用户
提问于 2015-02-19 23:35:42
回答 1查看 1.3K关注 0票数 0

我正在尝试使用OmniAuth将Facebook与我的网站整合,我想我在这里遇到了一些错误。现在,当我点击“使用Facebook登录”的时候,它确实把我带到了Facebook,但是我一登录就会发现说ActiveModel::ForbiddenAttributesError的错误。另外,我认为我的路线也有问题,但我不确定。

另外,我遵循了本RailsCasts教程:http://railscasts.com/episodes/360-facebook-authentication?autoplay=true

编辑:错误出现在这里,where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|

omniauth.rb

代码语言:javascript
复制
OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
end

user.rb

代码语言:javascript
复制
class User < ActiveRecord::Base
  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.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end

routes.rb

代码语言:javascript
复制
Rails.application.routes.draw do

  get 'auth/:provider/callback', to: 'sessions#create'
  get 'auth/failure',  ('/posts/index')
  get 'signout', to: 'sessions#destroy', as: 'signout'

  resources :welcome
  resources :posts

  root "welcome#index"

sessions_controller.rb

代码语言:javascript
复制
class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end

application_controller.rb

代码语言:javascript
复制
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  private

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-20 00:10:03

修改您的查找器如下:

代码语言:javascript
复制
class User < ActiveRecord::Base
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_initialize do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28619109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档