简体   繁体   中英

Rails User user avatar profil picture, in product listing

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

I think you are missing "=" in

<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %> 

add

<%= cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %> 

it may resolve your issue.

Edit:

Please refer this link to understand the syntax difference in erb. difference

what does

<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %> 

return? Does it return a url? If it does, you need to use an image_tag to display it.

<%=image_tag cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM