我正在制作一个小型应用程序,需要能够将图像上传到帖子/广告然后显示该图像。
我正在使用'Paperclip' gem来处理图片上传。 每当我创建帖子/广告时,都会显示默认的“missing.jpg”,而不是我的图片。
广告控制器
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
广告_形式视图
<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 %>
广告模型
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
广告索引视图
<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 %>
非常感谢任何帮助。感谢
编辑: 这是Google Chrome控制台中的错误。
答案 0 :(得分:1)
问题在于您允许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
我不确定你为什么在这里images_attributes: [:preview]
。方法定义应该只是:
def ad_params
params.require(:ad).permit(:title, :url, :preview)
end
您没有收到上传图片并获得默认missing.jpg
的原因是因为您未在:preview
ad_params
{{}}}中url
创建了您在表单中选择的图片文件1}}方法。
<强>更新强>
就路径而言,您应该能够将path
和has_attached_file
等其他参数提供给# 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"
。如下所示:
public/assets/ads/preview/:id/:style/
使用此功能,您上传的所有预览文件都将位于{{1}}目录中。