我在 Rails Facebook 登录时做错了什么 Ruby
What am I doing wrong Ruby on Rails facebook login
这是我的用户模型,名为 "user.rb",我希望人们可以使用他们的 Facebook 帐户登录并将他们的个人资料图片传输到我的网站。问题是它一直说 "profile_picture" 变量中的 "user.image" 未定义。 Ruby 用回形针存储图像 gem。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable
validates :fullname, presence: true, length: {in: 2..50}
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.fullname = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0,20]
end
end
end
def profile_picture
if user.image
user.image
else
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
end
end
在我的 html.erb 文件中,我调用了图像方法:
<%= image_tag current_user.avatar.url(:thumb) %>
我知道我必须将其更改为:
<%= image_tag current_user.profile_picture %>
这是我的 schema.rb 文件:
ActiveRecord::Schema.define(version: 20150927111830) do
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "fullname"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "provider"
t.string "uid"
t.string "image"
end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
profile_picture
方法中的 user
超出范围。每当你在你的方法中声明一个局部变量时(例如 user
),它只会在该方法的范围内可用。
current_user
是 User
的一个实例,因此如果您想要该用户的图像,只需调用 image_tag
:
<%= image_tag(current_user.image) %>
这是我的用户模型,名为 "user.rb",我希望人们可以使用他们的 Facebook 帐户登录并将他们的个人资料图片传输到我的网站。问题是它一直说 "profile_picture" 变量中的 "user.image" 未定义。 Ruby 用回形针存储图像 gem。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable
validates :fullname, presence: true, length: {in: 2..50}
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.fullname = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0,20]
end
end
end
def profile_picture
if user.image
user.image
else
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
end
end
在我的 html.erb 文件中,我调用了图像方法:
<%= image_tag current_user.avatar.url(:thumb) %>
我知道我必须将其更改为:
<%= image_tag current_user.profile_picture %>
这是我的 schema.rb 文件:
ActiveRecord::Schema.define(version: 20150927111830) do
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "fullname"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "provider"
t.string "uid"
t.string "image"
end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
profile_picture
方法中的 user
超出范围。每当你在你的方法中声明一个局部变量时(例如 user
),它只会在该方法的范围内可用。
current_user
是 User
的一个实例,因此如果您想要该用户的图像,只需调用 image_tag
:
<%= image_tag(current_user.image) %>