上传后{car}图片未显示

时间:2017-05-31 23:50:42

标签: ruby-on-rails image carrierwave

所以,我使用了CarrierWave gem并使用了一个railscast演示来使它工作,但图片没有显示在我的列表显示页面上。

这是显示页面上的代码:

<p id="notice"><%= notice %></p>

<p>
  <strong>Description:</strong>
  <%= @listing.description %>
</p>

<p>
  <strong>Price $:</strong>
  <%= @listing.price %>
</p>


<p>
  <strong>Subcategory:</strong>
  <%= Subcategory.find(@listing.subcategory_id).subcategory_name %>
</p>

<p>
  <strong>Choose an image to upload:</strong>
  <%= image_tag @listing.image_url(:half).to_s %>
</p>


<%= link_to 'Edit', edit_listing_path(@listing) %> |
<%= link_to 'Back', listings_path %> |
<%= link_to 'Home', root_path %>

这是控制器:

class ListingsController < ApplicationController
  before_action :set_listing, only: [:show, :edit, :update, :destroy]
  before_action :set_options, only: [:new, :edit, :update, :create]
  # GET /listings
  # GET /listings.json
def search
  if params[:category_id].present?
    @category = Category.find(params[:category_id])
    @subcategory = nil
elsif params[:subcategory_id].present?
    @subcategory = Subcategory.find(params[:subcategory_id])
    @category = Category.find(@subcategory.category_id)
end
  end

  def index
    @listings = Listing.all
  end

  # GET /listings/1
  # GET /listings/1.json
  def show
  end



  # GET /listings/new
  def new

    @listing = Listing.new
  end

  # GET /listings/1/edit
  def edit
  end





  # POST /listings
  # POST /listings.json
  def create
    @listing = Listing.new(listing_params)

    respond_to do |format|
      if @listing.save
        format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
        format.json { render :show, status: :created, location: @listing }
      else
        format.html { render :new }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /listings/1
  # PATCH/PUT /listings/1.json
  def update
    respond_to do |format|
      if @listing.update(listing_params)
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { render :show, status: :ok, location: @listing }
      else
        format.html { render :edit }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
    @listing.destroy
    respond_to do |format|
      format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_listing
      @listing = Listing.find(params[:id])
    end

    def set_options
      @options_for_select_ary = Subcategory.all.collect {|subcategory| [ subcategory.subcategory_name, subcategory.id ] }

    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def listing_params
      params.require(:listing).permit(:id, :description, :price, :subcategory_id)
    end
end

最后是模型:

class Listing < ApplicationRecord
  attr_accessor :image, :remote_image_url

  belongs_to :category, optional: true
  belongs_to :subcategory, optional: true
  belongs_to :delivery, optional: true
  belongs_to :user, optional: true


  mount_uploader :image, AvatarUploader
end

所以,我的问题是,如何在列表创建后将图像显示在列表中?

如果您需要查看载波代码,我可以在编辑中添加我的AvatarUploader类....

0 个答案:

没有答案