1

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

Ajay Barot
  • 1,594
  • 1
  • 22
  • 35
Tomas
  • 177
  • 11
  • can you please give some more details like html, model .. – Zero Nov 02 '16 at 09:18
  • Hello, This is my listing model 'class Service < ApplicationRecord belongs_to :user has_many :orders validates :user, presence: true validates :name, presence: true validates :city, presence: true validates :price, presence: true, numericality: { greater_than: 0} validates :category, presence:true, inclusion: { in: %w(Check-in Check-out Limpieza), message: "%{value} no es una entrada correcta" } #validates :rating, inclusion: {in: [0, 1, 2, 3, 4, 5]} end' – Tomas Nov 02 '16 at 09:20

2 Answers2

0

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

Community
  • 1
  • 1
Zero
  • 606
  • 5
  • 19
  • Thanks Prakash, on show its working fine, but on the index page i got an error: ActiveRecord::RecordNotFound in ListingsController#index Couldn't find Service with 'id'= services = Service.all end seller = Service.find(params[:id]).user end def show – Tomas Nov 02 '16 at 09:52
  • @Tomas, are you sending params[:id] in yours params while accessing params? – Zero Nov 02 '16 at 10:12
  • what do you mean exactly ? – Tomas Nov 02 '16 at 10:21
  • oh sorry can you please add params log for index method call? – Zero Nov 02 '16 at 10:22
  • Actually it say: Request Parameters: None So i think the problem is here – Tomas Nov 02 '16 at 10:23
  • i've update my question with the full controller listing (call service in the example) – Tomas Nov 02 '16 at 11:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127178/discussion-between-prakash-s-and-tomas). – Zero Nov 02 '16 at 12:03
0

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) %>
Cedric Loy
  • 51
  • 1
  • 7