为什么在使用 Active Storage gem 上传图像后无法获取图像?
Why can't I fetch images after uploading them with the Active Storage 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)
这是查看页面的代码 link:https://gist.github.com/Josebuendia/21ba9b9a7e6b67bf593ed44675fbb97c
以及获取数据的控制器:https://gist.github.com/Josebuendia/b22486363fdffe470b27b34daad2cc9b
用户=User.last
用户负载 (0.8ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["限制", 1]]
=> #<用户 ID:18,电子邮件:“farmer12@gmail.com”,created_at:“2020-08-05 16:47:08”,updated_at:“2020-08 -06 17:56:28", role: 1, farm_name: "Glen Valley Farm", farmers_picture: nil, about_farm: "我的农场出售有机 artisan 牛肉和羊肉。我是...", farm_type: "Mixed", county: "Wicklow">
irb(main):013:0> user.avatar.attached?
ActiveStorage::Attachment 加载(3.1 毫秒)SELECT "active_storage_attachments".* 从 "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ?
AND "active_storage_attachments"."name" = ?限制 ? [["record_id", 18], ["record_type", "用户"], ["姓名", "头像"], ["LIMIT", 1]]
=> 真
irb(主要):014:0>
ents" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ?
AND "active_storage_attachments"."name" = ?限制 ? [["record_id", 18], ["record_type", "用户"], ["姓名", "头像"], ["LIMIT", 1]]
=> 真
奇怪的是,数据与项目一起正确获取,即使它也使用活动存储 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 添加到 select(不带 :avatar)
def farmers
#@users = User.where(role: 1)
@users = User.select(:id, :farm_name, :about_farm, :farm_type, :county).where(role: 1)
end
注意 :avatar
不是必需的,因为它实际上不是数据库中的列。
我也在用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)
这是查看页面的代码 link:https://gist.github.com/Josebuendia/21ba9b9a7e6b67bf593ed44675fbb97c
以及获取数据的控制器:https://gist.github.com/Josebuendia/b22486363fdffe470b27b34daad2cc9b
用户=User.last
用户负载 (0.8ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["限制", 1]]
=> #<用户 ID:18,电子邮件:“farmer12@gmail.com”,created_at:“2020-08-05 16:47:08”,updated_at:“2020-08 -06 17:56:28", role: 1, farm_name: "Glen Valley Farm", farmers_picture: nil, about_farm: "我的农场出售有机 artisan 牛肉和羊肉。我是...", farm_type: "Mixed", county: "Wicklow">
irb(main):013:0> user.avatar.attached?
ActiveStorage::Attachment 加载(3.1 毫秒)SELECT "active_storage_attachments".* 从 "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ?
AND "active_storage_attachments"."name" = ?限制 ? [["record_id", 18], ["record_type", "用户"], ["姓名", "头像"], ["LIMIT", 1]]
=> 真
irb(主要):014:0>
ents" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ?限制 ? [["record_id", 18], ["record_type", "用户"], ["姓名", "头像"], ["LIMIT", 1]] => 真
奇怪的是,数据与项目一起正确获取,即使它也使用活动存储 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 添加到 select(不带 :avatar)
def farmers
#@users = User.where(role: 1)
@users = User.select(:id, :farm_name, :about_farm, :farm_type, :county).where(role: 1)
end
注意 :avatar
不是必需的,因为它实际上不是数据库中的列。