为什么使用活动存储 Gem 上传图像后无法获取图像?



我也在使用Devise。我更新了卖家,卖家页面上没有出现头像。这种情况已经持续了一段时间。

这是终端中的服务器输出:

Started GET "/farmers" for 127.0.0.1 at 2020-08-06 19:26:57 +0100
(0.5ms)  SELECT sqlite_version(*)
Processing by UserController#farmers as HTML
Rendering user/farmers.html.erb within layouts/application
User Load (2.6ms)  SELECT "users"."farm_name", "avatar", "users"."about_farm", "users"."farm_type", "users"."county" FROM "users" WHERE "users"."role" = ?  [["role", 1]]
↳ app/views/user/farmers.html.erb:6
ActiveStorage::Attachment Load (2.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" IS NULL AND "active_storage_attachments"."record_type" 
= ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]]
↳ app/views/user/farmers.html.erb:28
CACHE ActiveStorage::Attachment Load (0.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" IS NULL AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]]
↳ app/views/user/farmers.html.erb:28
Rendered user/farmers.html.erb within layouts/application (Duration: 848.9ms | Allocations: 13223)
[Webpacker] Everything's up-to-date. Nothing to do
User Load (1.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? 
[["id", 18], ["LIMIT", 1]]
↳ app/views/layouts/application.html.erb:47
Rendered layouts/_search.html.erb (Duration: 1.3ms | Allocations: 173)
Completed 200 OK in 1297ms (Views: 1217.8ms | ActiveRecord: 30.9ms | Allocations: 21165)

以下是视图页面代码的链接:https://gist.github.com/Josebuendia/21ba9b9a7e6b67bf593ed44675fbb97c

以及获取数据的控制器:https://gist.github.com/Josebuendia/b22486363fdffe470b27b34daad2cc9b

user=user.last用户负载(0.8ms(选择";用户".*FROM";用户";ORDER BY";用户"id";DESC限制?[["极限",1]]
=><用户id:18,电子邮件:";farmer12@gmail.com&";,created_at:";2020-08-05 16:47:08";,updated_at:";2020-08-06 17:56:28";,角色:1,农场名称:"Glen Valley Farm";,farmers_picture:nil,about_farm:"我的农场出售有机手工牛肉和羊肉。我是;,farm_type:";"混合";,县:";Wicklow">irb(主(:013:0>user.avatar.attached?ActiveStorage:附件加载(3.1ms(SELECT";active_storage_attachments"。*FROM";active_storage_attachments";其中";active_storage_attachments"record_id"=?"与";active_storage_attachments"record_type"=?"与";active_storage_attachments"name"=?限制?[["record_id",18],[["record_type","User"],["name","avatar"],"LIMIT",1]]=>真的irb(主(:014:0>

ents";其中";active_storage_attachments"record_id"=?"与";active_storage_attachments"record_type"=?"与";active_storage_attachments"name"=?限制?[["record_id",18],[["record_type","User"],["name","avatar"],"LIMIT",1]]=>真实

奇怪的是,数据是用项目正确提取的,尽管这也使用了Active Storage gem!

项目表单中的表单元素就是表单。

在用户的其他形式中,它是f.

不过,我怀疑这有什么不同。

查看页面代码:

<p id="notice"><%= notice %></p>
<div id="itemsContainer">
<h1>Our Farmers</h1>
<% @users.each do |user| %>
<div class="itemhols">
<%
=begin%>
<h1><%= user.email %></h1>
<%
=end%>
<!--Data to include on farmers: name, pic, about, type, county-->
<h2><%= user.farm_name %></h2>
<%
=begin%>
<p><%= image_tag user.farmers_picture if user.farmers_picture.present? %></p> 
<%
=end%>
<%
=begin%>
<%= image_tag user.avatar.variant(resize: "200x200").processed if user.avatar.attached? %> 
<%
=end%>
<% if user.avatar.attached? %>
<image src="<%=(url_for(user.avatar))%>" class="itemholsIm">
<% end %>
<p><%= user.about_farm %></p>
<p><%= user.farm_type %></p>
<p><%= user.county %></p>

<%
=begin%>
<%= link_to 'Show', user, :class => "button", :role => "button" %>  
<%
=end%>
<%
=begin%>
<--<%= link_to 'Show', item, :class => "button", :role => "button" %> 
<%
=end%>
</div>
<% end %>
</div>

以及获取数据的控制器:

class UserController < ApplicationController
def login
session[:login] = 1
session[:cart] = nil
#Changed the line below to Seller Login sucessfull! from "Admin Login sucessfull!"
flash[:notice] = "Seller Login sucessfull!"
redirect_to :controller => :items
end
def logout
session[:login] = nil
session[:cart] = nil
flash[:notice] = "You have been successfully logged out!!"
redirect_to :controller => :items
end
#iteration for farmers page
def farmers
#@users = User.where(role: 1)
@users = User.select(:farm_name, :avatar, :about_farm, :farm_type, :county).where(role: 1)
end
def search
st = "%#{params[:q]}%"
@users = User.where("email like ?", st)
end
def show
end
#def farmers
#   @user = User.find(params[:id])
# FIXME get the view working for the farmers page
#end
def upgrade_admin
@user.update_attribute(:adminrole, true)
redirect_to :action => :admin_users
end

def downgrade_admin
@user.update_attribute(:adminrole, false)
redirect_to :action => :admin_users
end    
end

这个方法很可能是罪魁祸首。。。尽管可能存在其他问题

def farmers
#@users = User.where(role: 1)
@users = User.select(:farm_name, :avatar, :about_farm, :farm_type, :county).where(role: 1)
end

因为ActiveStorage附件是多主体关系,所以您需要(至少(在SELECT中包含用户id。根据你的措辞,它似乎也起过作用,但在某个时候停止了作用。我的猜测是,当你评论掉这句话时,它停止了工作。。。

@users = User.where(role: 1)

我建议只加载整个用户,以避免将来出现问题。。。

def farmers
@users = User.where(role: 1)
end

或者,将:id添加到选择(不包含:avatar(

def farmers
#@users = User.where(role: 1)
@users = User.select(:id, :farm_name, :about_farm, :farm_type, :county).where(role: 1)
end

注意,不需要:avatar,因为它实际上不是数据库中的一列。

最新更新