【问题标题】:Rails User user avatar profil picture, in product listingRails 用户用户头像个人资料图片,在产品列表中
【发布时间】:2016-11-02 19:56:21
【问题描述】:

我想在列表的索引和显示页面中使用卖家的头像

所以我把我的列表控制器:


这是列表的完整控制器(本例中调用服务)

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

在此先感谢

【问题讨论】:

  • 能否请您提供更多详细信息,例如 html、模型 ..
  • 您好,这是我的列表模型类服务,存在:真,数值:{ 大于:0} 验证:类别,存在:真,包含:{ 在:%w(签入签出 Limpieza),消息:“%{value} no es una entrada correcta” } #validates :rating, 包含:{in: [0, 1, 2, 3, 4, 5]} end'

标签: ruby-on-rails cloudinary


【解决方案1】:

我认为您在

中缺少“=”
<% cloudinary_url(@seller.avatar.path , width: 50, height: 50, crop: :fill) %> 

添加

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

它可能会解决您的问题。

编辑:

请参阅此链接以了解 erb 中的语法差异。 difference

【讨论】:

  • 感谢 Prakash,显示其工作正常,但在索引页面上我收到错误:ActiveRecord::RecordNotFound in ListingsController#index 找不到具有 'id'= services = Service.all 的服务最终卖家 = Service.find(params[:id]).user end def show
  • @Tomas,您在访问参数时是否在您的参数中发送 params[:id]?
  • 你到底是什么意思?
  • 哦,抱歉,能否为索引方法调用添加参数日志?
  • 实际上它说:请求参数:无所以我认为问题出在这里
【解决方案2】:

做什么

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

返回?它会返回一个网址吗?如果是,则需要使用 image_tag 来显示它。

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

【讨论】:

  • 我将其更改为 现在在显示页面上它的工作但我得到一个列表索引页面上的错误“找不到带有 'id'=" 的列表
  • 所以我的解决方案是针对显示页面:

    没有图片

猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 2014-10-05
  • 2013-04-13
  • 2015-11-30
相关资源
最近更新 更多