0
votes

I am also using Devise. I update the seller and the avatar image doesn't appear on the sellers page. This has been going on a while.

This is the server output in the terminal:

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)

Here is a link to the code for the view page: https://gist.github.com/Josebuendia/21ba9b9a7e6b67bf593ed44675fbb97c

And the controller that fetches the data: https://gist.github.com/Josebuendia/b22486363fdffe470b27b34daad2cc9b

user = User.last User Load (0.8ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<User id: 18, email: "[email protected]", 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: "My farm sells organic artisan beef and lamb. I'm l...", farm_type: "Mixed", county: "Wicklow"> irb(main):013:0> user.avatar.attached? ActiveStorage::Attachment Load (3.1ms) 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", 18], ["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]] => true irb(main):014:0>

ents" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 18], ["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]] => true

The weird thing is that the data is fetched correctly with the items even though that's also using the Active Storage gem!

The form elements in the items form is form.

In the other forms for the users it is f.

Though, I doubt that makes a difference.

The view page code:

<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>

And the controller that fetches the data:

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
1
Welcome to SO. One clue from the log: WHERE "active_storage_attachments"."record_id" IS NULL is not a recipe for success, so a good place to start would be ensuring that the avatar references are being saved/used correctly. To get better/quicker help, it is good to provide a minimal, reproducible example of the problem, or at least some curated snippets from your code. In your case, probably how the avatar image is saved and your #farmers method at a minimum.rmlockerd
Hopefully, this is enough info @rmlockerdFintan Moloney
@dbugger Inline? Anything in particular I should show?Fintan Moloney

1 Answers

1
votes

This method is most likely the culprit...although there may be other issues

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

Because ActiveStorage attachments are polymorpic relationships, you need to (at the very least) include the user id in the SELECT. It also seems like, based on your wording, that it worked once but at some point stopped working. My guess is it stopped working when you commented out this line...

@users = User.where(role: 1)

I would suggest just loading the entire user to avoid future problems...

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

Or, add :id to the select (without :avatar)

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

Note :avatar is not needed because it isn't actually a column in the database.