我正在尝试在用户展示页面中上传用户照片,这对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)
我知道它正在击中我的userscontroller中的update方法,因为通知闪烁着“ Not Uploaded”,但是服务器日志似乎暗示它正在执行get请求而不是put命令?我想实现这一目标而无需任何宝石。
答案 0 :(得分:0)
解决方案是更改粗体:
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