将用户头像添加到Devise的RailsActiveStorage示例_Ruby_H.zip


2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
在Ruby on Rails开发中,Devise是一个非常流行的用户认证库,它简化了用户注册、登录、密码重置等过程。而ActiveStorage是Rails 5.2及更高版本中引入的一个特性,用于处理文件上传和存储,包括用户头像。本示例将详细解释如何将ActiveStorage集成到使用Devise的Rails应用中,以便用户可以上传和管理他们的个人头像。 我们需要确保项目已经安装了Devise和ActiveStorage。在Gemfile中添加以下行: ```ruby gem 'devise' gem 'rails', '~> 5.2' # 或者更高版本,以包含ActiveStorage ``` 然后运行`bundle install`来安装这些依赖。 接下来,我们需要创建一个User模型,并配置Devise以包含头像字段。打开`db/migrate/创建_users.rb`(或相应的新迁移文件)并添加`image`字段: ```ruby class CreateUsers < ActiveRecord::Migration[5.2] def change create_table :users do |t| t.string :email, null: false, default: '' t.string :encrypted_password, null: false, default: '' t.string :reset_password_token t.datetime :reset_password_sent_at t.datetime :remember_created_at t.datetime :sign_in_at t.datetime :current_sign_in_at t.inet :current_sign_in_ip t.inet :last_sign_in_ip t.string :image t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true end end ``` 运行`rails db:migrate`来执行这个迁移。 在`app/models/user.rb`中,添加`has_one_attached :image`来启用ActiveStorage: ```ruby class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_one_attached :image end ``` 现在我们已经为用户模型添加了头像字段,接下来要在视图中显示上传界面。在Devise的注册或编辑用户视图中(通常是`app/views/devise/registrations/edit.html.erb`或`app/views/devise/registrations/new.html.erb`),添加以下代码来显示上传表单: ```erb <%= form_with(model: resource, local: true) do |form| %> <%= form.label :image %> <%= form.file_field :image %> # 其他字段... <% end %> ``` 别忘了在控制器中允许`image`参数: ```ruby class RegistrationsController < Devise::RegistrationsController # ... protected def sign_up_params params.require(:user).permit(:email, :password, :password_confirmation, :image) end def account_update_params params.require(:user).permit(:email, :password, :password_confirmation, :current_password, :image) end end ``` 为了在用户详情页面展示头像,可以在`app/views/devise/registrations/show.html.erb`或相应的视图中添加: ```erb <% if @user.image.attached? %> <%= image_tag @user.image %> <% end %> ``` 确保配置好ActiveStorage的存储服务,例如Amazon S3或Google Cloud Storage。在`config/storage.yml`中添加对应的配置,并在`config/environments/development.rb`、`config/environments/production.rb`等环境中启用。 通过以上步骤,你就成功地将用户头像功能集成到了使用Devise的Rails应用中,利用了ActiveStorage的强大功能。用户现在可以上传和查看自己的头像,同时,应用也能够有效地管理和存储这些图像文件。






























































































































- 1
- 2


- 粉丝: 2w+
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 软件工程项目售后维护方案.docx
- P2P电子商务背景下的动态安全信任管理机制.doc
- 单片机甲醛检测仪设计方案.doc
- 破坏计算机信息系统罪中严重后果如何把握.docx
- 计算机网络安全技术在企业网的应用与研究.docx
- 计算机网络安全的实际状况及其应对策略探讨.docx
- 光电传感器转速测量系统设计方案单片机光电转速传感器转速测量数据处理.doc
- 互联网金融对大学生支付方式和理财行为的影响.docx
- 互联网+时代下的中职学校主题班会.docx
- itat-全国信息技术应用大赛第二届java历年真题.doc
- 互联网金融相关法律法规存在问题及完善方案.docx
- 大型Android项目基础架构,AndroidX、Jetpack、Koin、模块化、模块代码权限控制、单Activity多fragment
- 中国-最好的人工智能生长土壤.docx
- 如何加强石油工程项目管理质量.docx
- 2011年全国计算机等级历年考试四级网络工程师历年考试复习题全省纸打印版免费.doc
- 刍议网络环境下图书采编工作的转变.docx


