简体   繁体   English

Rails回形针-无法正确上传图像

[英]Rails Paperclip - Cannot upload image correctly

I am making a small rails application that requires being able to upload an image to a post/ad and then displaying that image. 我正在制作一个小型Rails应用程序,该应用程序需要能够将图像上传到帖子/广告,然后显示该图像。

I am using the 'Paperclip' gem to handle the image uploads. 我正在使用“ Paperclip” gem处理图像上传。 Whenever I create my post/ad the default 'missing.jpg' is displayed, not my image. 每当我创建我的帖子/广告时,都会显示默认的“ missing.jpg”,而不是我的图片。

Ads Controller 广告控制器

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # GET /ads
  # GET /ads.json
  def index
    @ads = Ad.all
  end

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

  # GET /ads/new
  def new
    @ad = Ad.new
  end

  # GET /ads/1/edit
  def edit
  end

  # POST /ads
  # POST /ads.json
  def create
    @ad = Ad.new(ad_params)

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

  # PATCH/PUT /ads/1
  # PATCH/PUT /ads/1.json
  def update
    respond_to do |format|
      if @ad.update(ad_params)
        format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ads/1
  # DELETE /ads/1.json
  def destroy
    @ad.destroy
    respond_to do |format|
      format.html { redirect_to ads_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def ad_params
      params.require(:ad).permit(:title, :url, images_attributes: [:preview])
    end
end

Ad _form View 广告_表格视图

<style type="text/css">
.header {
  display: none!important;
}
</style>

<%= form_for @ad, :html => {:multipart => true} do |f| %>
  <% if @ad.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@ad.errors.count, "error") %> prohibited this ad from being saved:</h2>

      <ul>
      <% @ad.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <span>Contact (URL, Mobile, Address)</span><br>
    <%= f.text_field :url %>
  </div>
  <div class="field">
    <%= f.label :image %><br>
    <%= f.file_field :preview %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Ad Model 广告模型

class Ad < ActiveRecord::Base

    attr_accessible :title, :url, :preview
    belongs_to :user
  has_attached_file :preview, :default_url => "missing.jpg"

  validates :title, length: { maximum: 20 }
  validates :url, length: { maximum: 20 }
end

Ad Index View 广告索引视图

<h1>Listing ads</h1>




    <% @ads.each do |ad| %>
    <div class="adspace_grid">
      <%= image_tag ad.preview.url %>
      <div class="text_info">
      <span class="bold"><%= ad.title %></span> / 
      <span class="bold">Contact : </span><%= ad.url %>
      </div>
    </div> 
    <%= link_to 'Delete', ad, :method => :delete %> 
    <% end %>


<br>

<%= link_to 'New Ad', new_ad_path %>

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

EDIT : This is the error in the Google Chrome Console. 编辑:这是Google Chrome控制台中的错误。 错误代码

The problem is where you are permitting ad attributes: 问题是您在哪里允许ad属性:

# Never trust parameters from the scary internet, only allow the white list through.
def ad_params
  params.require(:ad).permit(:title, :url, images_attributes: [:preview])
end

I'm not sure why you have images_attributes: [:preview] here. 我不确定为什么在这里有images_attributes: [:preview] The method definition should just be: 方法定义应为:

def ad_params
  params.require(:ad).permit(:title, :url, :preview)
end

The reason you are not getting the uploaded picture and are getting the default missing.jpg is because the your image file selected in the form did not get created because you were not permitting :preview in your ad_params method. 您没有获得上传的图片并获得默认的missing.jpg的原因是,由于您不允许ad_params方法中的:preview ,因此未创建在表单中选择的图像文件。

Update: 更新:

As far as the path is concerned, you should be able to supply other parameters uch as url and path to has_attached_file . 就路径而言,您应该能够提供其他参数,例如urlhas_attached_file path Something like following: 如下所示:

# app/models/ad.rb

has_attached_file :preview, :default_url => "missing.jpg", 
                  :url  => "/assets/ads/preview/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/preview/:id/:style/:basename.:extension"

With this all your uploaded preview files will be in public/assets/ads/preview/:id/:style/ directory. 这样,您所有上传的预览文件都将位于public/assets/ads/preview/:id/:style/目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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