ruby-on-rails - 无法上传用户个人资料照片 rails 5(没有宝石)
问题描述
我正在尝试在用户展示页面中上传用户照片,这对 Rails 项目来说相对较新。我将包括我的用户显示 html erb 页面,以及我的用户控制器和用户模型,以及当我按下“更新”按钮时服务器正在做什么。
class UsersController < ApplicationController
skip_before_action :verify_authentication, only: [:new, :create, :index]
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to root_path, notice: "User successfully created."
else
redirect_to new_user_path, notice: "Something went wrong, please try again."
end
end
def index
end
def update
if current_user != @user_id
flash[:notice] = "You cannot update this user"
end
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:notice] = "Profile updated"
redirect_to @user
else
flash[:notice] = "Not uploaded"
redirect_to @user
end
end
def new
@user = User.new
end
def show
@user = User.find(params[:id])
@posts = @user.posts
end
def destroy
if current_user == @user
@user.destroy
else
render json: @user.errors, status: :unprocessable_entity
end
end
private
def user_params
params.permit(:username, :password, :photo)
end
end
用户模型
class User < ApplicationRecord
has_secure_token :api_token
has_secure_password
validates :username, uniqueness: true
validates :password, presence: true, length: {minimum: 5}
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_one_attached :photo
end
用户 show.html.erb 页面
<h1 class='h3 mt-5 mb-3 font-weight-normal text-center'><%= @user.username %> </h1>
<% if notice %>
<p><%= notice %></p>
<% end %>
<% if @user.photo.attached? %>
<p>
<strong> Profile Photo </strong>
<%= image_tag @user.photo, style: "max-width: 100px, max-height: 100px" %>
</p>
<% end %>
<div class="text-center">
<p> Upload a profile photo: </p>
<%= form_with model: @user, local:true do |form| %>
<%= form.label :photo %>
<%= form.file_field :photo %>
<%= form.submit %>
<% end %>
</div>
<div>
<%= link_to 'Back', root_path %>
</div>
<div>
<% @posts.each do |post| %>
<div class="card border-secondary text-center" style="width: 30rem;">
<div class="card-body">
<h3 class="card-title">
<%= post.question %>
</h3>
</div>
<p class="card-text">
<%= post.body %>
</p>
<p class="card-text">
<%= link_to 'Show', post_path(post) %>
</p>
<% end %>
</div>
最后是我点击提交时的服务器。
Started GET "/users/180" for 127.0.0.1 at 2018-09-17 14:45:03 -0400
Processing by UsersController#show as HTML
Parameters: {"id"=>"180"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 180], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:18
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 180], ["LIMIT", 1]]
↳ app/controllers/users_controller.rb:37
Rendering users/show.html.erb within layouts/application
ActiveStorage::Attachment Load (0.2ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 180], ["record_type", "User"], ["name", "photo"], ["LIMIT", 1]]
↳ app/views/users/show.html.erb:8
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 180]]
↳ app/views/users/show.html.erb:28
Rendered users/show.html.erb within layouts/application (4.5ms)
Completed 200 OK in 43ms (Views: 36.9ms | ActiveRecord: 0.9ms)
我知道它正在我的用户控制器中使用更新方法,因为通知正在闪烁“未上传”,但服务器日志似乎表明它正在执行获取请求而不是放置?我想在没有任何宝石的情况下实现这一目标。
解决方案
解决方案是粗体的变化:
def update
if current_user.id != @user_id
flash[:notice] = "You cannot update this user"
end
@user = User.find(params[:id])
**if @user.photo.attach(user_params[:photo])**
flash[:notice] = "Profile updated"
redirect_to @user
else
flash[:notice] = "Not uploaded"
redirect_to @user
end
end
并且comment_params 调整为:
def user_params
params.require(:user).permit(:username, :password, :photo,
:email)
end
推荐阅读
- c# - 为什么在 .net5 上运行的 Function App 报告缺少/空的服务总线连接字符串?
- c# - 如何制作一个控制windows音量的TrackBar C#
- python-3.x - Python Pandas 从源(单列)获取数据以纠正表格格式
- .net - 如何在 .net 网站项目中使用 2 个不同的包版本
- java - 如何在对应java语法的python中实现相同的for循环
- sql - SQL查找使用同一帐户的多个用户
- python - 训练 keras LSTM 模型时“无法将 NumPy 数组转换为张量”
- python - AttributeError:如果 DataFrame 被腌制为根,则无法获取属性“new_block”
- java - 无法在 java servlet 和 mysql 连接中保存记录
- python - DMOJ COCI'17 比赛 1 #1 Cezar