Rails 3使用OmniAuth未知属性错误

时间:2014-11-16 14:39:49

标签: ruby-on-rails ruby ruby-on-rails-3 omniauth restful-authentication

我正在尝试集成OmniAuth,以提供一种自包含的方式来验证用户身份。到目前为止,我可以在我的应用程序前面打开登录/注册对话框。但是,当我尝试注册新用户时,出现以下错误:


ActiveRecord::UnknownAttributeError

Request

Parameters:

{"name"=>"jed",
 "email"=>"clampett@hillbilly.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"}

我不确定在哪里查找问题。 OmniAuth的添加遵循O'Reilly书籍Learning Rails 3中的教程。我有以下与身份验证相关的模型:

... user.rb

    class User < ActiveRecord::Base
      attr_accessible :name, :provider, :uid

      def User.create_with_omniauth(auth) 
        user = User.new()
        user.provider = auth["provider"]
        user.uid = auth["uid"]
        user.name = auth["info"]["name"]
        user.save
        return user
       end
    end

... identity.rb

    class Identity < ActiveRecord::Base
      attr_accessible :email, :name, :password_digest, :password, :password_confirmation
    end

我已将omniauth.rb文件添加到“initializers”目录...

    Rails.application.config.middleware.use OmniAuth::Builder do
      provider :identity
    end

我还创建了sessions_controller ....

    class SessionsController < ApplicationController
      def create
        auth = request.env["omniauth.auth"]
        if (User.find_by_provider_and_uid(auth["provider"], auth["uid"]))
        user = User.find_by_provider_and_uid(auth["provider"], auth["uid"])
        else 
          user = User.create_with_omniauth(auth)
        end
        session[:user_id] = user.id
        redirect_to root_url, :notice => "Welcome!"
      end

      def destroy
        session[:user_id] = nil
        redirect_to root_url, :notice => "Goodbye!"
      end
    end

另外,我在routes.rb文件中添加了以下内容......

  match "/auth/:provider/callback" => "sessions#create"
  match "/signout" => "sessions#destroy", :as => :signout

这是架构......

ActiveRecord::Schema.define(:version => 20141116130255) do

  create_table "identities", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

  create_table "todos", :force => true do |t|
    t.string   "task_name"
    t.date     "completion_date"
    t.boolean  "is_completed"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "provider"
    t.string   "uid"
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

end

身份验证部分的部分视图:

<p>
    <% if current_user %>
        Welcome <%= current_user.name %>!
        | <%= link_to "Sign Out", signout_path %>
    <% else %>
        <%= link_to "Log in", "/auth/identity" %> |
        <%= link_to "Register", "/auth/identity/register" %>
    <% end %>
</p>
<hr />

这是一个简单的待办事项列表应用程序,我正在尝试添加身份验证。一旦我修复了这个错误,我就要为它添加一个priveleges结构。 这个错误意味着什么,我该如何解决?非常感谢能帮助我指明正确方向的人。

1 个答案:

答案 0 :(得分:0)

感谢所有回复的人。特别是安德烈 - 那个railscast链接确实有帮助。我已经解决了这个问题。将缺少的密码和password_confirmation字段添加到身份模型中,但最重要的是,我忘记了类本身是一类Omniauth。只需更改模型:

    class Identity < ActiveRecord::Base
        attr_accessible :email, :name, :password_digest, :password, :password_confirmation
    end

要:

    class Identity < OmniAuth::Identity::Models::ActiveRecord
        attr_accessible :email, :name, :password_digest, :password, :password_confirmation
    end

现在完美运作:)再次感谢所有人!