Rails 用户用户头像头像,在产品listing中
Rails User user avatar profil picture, in product listing
我想在listing的index和show页中使用卖家的头像
所以我把我的上市控制器:
这是列表的完整控制器(在本例中调用服务)
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
并且在展示和索引页面中我添加了这个:
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
在我的导航栏中,头像可以正常工作:
<%= cl_image_tag current_user.avatar.path, width: 50, height: 50, gravity: :face, crop: :thumb %>
非常感谢
我认为您在
中缺少“=”
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
添加
<%= cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
它可能会解决您的问题。
编辑:
请参阅此link以了解 erb 中的语法差异。 difference
什么
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
return?它 return 是 url 吗?如果是,则需要使用 image_tag 来显示它。
<%=image_tag cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
我想在listing的index和show页中使用卖家的头像
所以我把我的上市控制器:
这是列表的完整控制器(在本例中调用服务)
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
并且在展示和索引页面中我添加了这个:
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
在我的导航栏中,头像可以正常工作:
<%= cl_image_tag current_user.avatar.path, width: 50, height: 50, gravity: :face, crop: :thumb %>
非常感谢
我认为您在
中缺少“=”<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
添加
<%= cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
它可能会解决您的问题。
编辑:
请参阅此link以了解 erb 中的语法差异。 difference
什么
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>
return?它 return 是 url 吗?如果是,则需要使用 image_tag 来显示它。
<%=image_tag cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>