i want to use the avatar of the seller in the index and show page of listings
So i put my listing controller:
this is the full controller for listing (call service in this example)
class ServicesController < ApplicationController
skip_before_action :authenticate_user!, only: [:index, :show]
before_action :find_service, only: [:show, :edit, :update]
before_filter :check_user, only: [:edit, :update, :show]
def seller
@services = Service.where(user: current_user).order("created_at DESC")
end
def index
if params[:category]
@services = Service.where(category: params[:category])
else
@services = Service.all
end
@seller = Service.find(params[:id]).user
end
def show
@seller = Service.find(params[:id]).user
end
def new
@service = Service.new
end
def create
@service = Service.new(service_params)
@service.user_id = current_user.id
if @service.save
redirect_to services_path
else
render :new
end
end
def edit
end
def update
if @service.user == current_user
@service.update(service_params)
redirect_to services_path
else
flash[:alert] = "Este no es su producto"
render :index
end
end
private
def service_params
params.require(:service).permit(:name, :city, :price, :category, :id)
end
def check_seller
@seller = Service.find(params[:user_id]).user
end
def find_service
@service = Service.find(params[:id])
end
def check_user
if current_user != @service.user
redirect_to root_url, alert: "No puede realizar esta accion"
end
end
end
and In the show and index page i add this :
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
In my navbar the avatar is working fine with:
<%= cl_image_tag current_user.avatar.path, width: 50, height: 50, gravity: :face, crop: :thumb %>
Many thanks in advance