[英]Multiple Uploads Rails Paperclip not Saving
I have a rails model 'Guitar' and a rails model 'Photo.' 我有一个Rails模型“ Guitar”和一个Rails模型“ Photo”。 I wanted to be able to attach multiple photos so I watched the railscasts for Paperclip, read the docs, etc. I've used Paperclip in the past to attach a single photo, but I wanted to do multiples this time.
我希望能够附加多张照片,所以我观看了Paperclip的花絮,阅读了文档等等。过去我曾经使用Paperclip附加一张照片,但是这次我想做多张照片。 I don't have a problem updating the model, and it doesn't error out when I attach sample photos, but when I rails console to Photos.all, nothing comes back.
我更新模型没有问题,并且在附加示例照片时也不会出错,但是当我将控制台导航到Photos.all时,什么也没回来。 My ultimate goal is to a polymorphic association with an 'Amps' model, but I really just wanted to get 'Guitars' working first.
我的最终目标是与“ Amps”模型建立多态关联,但我真的只是想首先让“ Guitars”工作。
I have paperclip added to the Gemfile and I've bundle installed, restarted the server, etc. I think I'm missing something stupid and I'm new to rails so please be gentle... 我已将回形针添加到Gemfile中,并且已安装捆绑包,重新启动服务器等。我认为我缺少一些愚蠢的东西,而且我是Rails的新手,所以请保持温柔...
guitar.rb 吉他
class Guitar < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb photo.rb
class Photo < ActiveRecord::Base
belongs_to :guitars
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>',
large: '600x600#' }
end
guitars_controller.rb guitars_controller.rb
class GuitarsController < ApplicationController
before_action :set_guitar, only: [:show, :edit, :update, :destroy]
# GET /guitars
# GET /guitars.json
def index
@guitars = Guitar.all
end
# GET /guitars/1
# GET /guitars/1.json
def show
end
# GET /guitars/new
def new
@guitar = Guitar.new
3.times { @guitar.photos.build }
end
# GET /guitars/1/edit
def edit
@guitar = Guitar.find(params[:id])
3.times { @guitar.photos.build }
end
# POST /guitars
# POST /guitars.json
def create
@guitar = Guitar.new(guitar_params)
@guitar.user_id = current_user.id if current_user
respond_to do |format|
if @guitar.save
format.html { redirect_to @guitar, notice: 'Guitar was successfully created.' }
format.json { render action: 'show', status: :created, location: @guitar }
else
format.html { render action: 'new' }
format.json { render json: @guitar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guitars/1
# PATCH/PUT /guitars/1.json
def update
respond_to do |format|
if @guitar.update(guitar_params)
format.html { redirect_to @guitar, notice: 'Guitar was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @guitar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guitars/1
# DELETE /guitars/1.json
def destroy
@guitar.destroy
respond_to do |format|
format.html { redirect_to guitars_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guitar
@guitar = Guitar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :one_owner, :user_id)
end
end
guitars show.html.erb 吉他show.html.erb
<p>
<strong>Make:</strong>
<%= @guitar.make %>
</p>
<p>
<strong>Model:</strong>
<%= @guitar.model %>
</p>
<p>
<strong>Year:</strong>
<%= @guitar.year %>
</p>
<p>
<strong>Color:</strong>
<%= @guitar.color %>
</p>
<p>
<strong>Serial:</strong>
<%= @guitar.serial %>
</p>
<p>
<strong>Price:</strong>
<%= @guitar.price %>
</p>
<p>
<strong>Condition:</strong>
<%= @guitar.condition %>
</p>
<p>
<strong>Kind:</strong>
<%= @guitar.kind %>
</p>
<p>
<strong>Bodykind:</strong>
<%= @guitar.bodykind %>
</p>
<p>
<strong>Frets:</strong>
<%= @guitar.frets %>
</p>
<p>
<strong>One owner:</strong>
<%= @guitar.one_owner %>
</p>
<p>
<strong>User:</strong>
<%= @guitar.user_id %>
</p>
<div class="thumb">
<% for asset in @guitar.photos %>
<%= link_to image_tag(photo.photos.url(:thumb)), photo.photo.url(:original) %>
<% end %>
</div>
<%= link_to 'Edit', edit_guitar_path(@guitar) %> |
<%= link_to 'Back', guitars_path %>
guitars _form.html.erb 吉他_form.html.erb
<%= simple_form_for(@guitar, html: { class: 'form-horizontal', :multipart => true}) do |f| %>
<% if @guitar.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@guitar.errors.count, "error") %> prohibited this guitar from being saved:</h2>
<ul>
<% @guitar.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :make, label: 'Make of the Guitar', placeholder: 'Gibson' %>
<%= f.input :model, label: 'Model of the Guitar', placeholder: 'Les Paul' %>
<%= f.input :year, label: 'Year Made', placeholder: '1957' %>
<%= f.input :color, label: 'Color', placeholder: 'GoldTop' %>
<%= f.input :serial, label: 'Serial', placeholder: '#7-8789' %>
<%= f.input :price, label: 'Price' %>
<%= f.input :condition, label: 'Condition, 1-10' %>
<%= f.input :kind, label: 'Kind of Guitar', placeholder: '6-String-Electric' %>
<%= f.input :bodykind, label: 'Body Type', placeholder: 'Solid String Electric' %>
<%= f.input :frets, label: 'Number of Frets', placeholder: '22' %>
<%= f.input :one_owner, label: 'Original Owner' %>
<%= f.fields_for :photos do |photo| %>
<% if photo.object.new_record? %>
<%= photo.file_field :photo %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :one_owner, :user_id, photos_attributes: [:photo])
end
You're running into mass assignment protection which is preventing the photos from being saved. 您正在遇到批量分配保护,这将阻止保存照片。 Add this line to your
Guitar
model: 将此行添加到您的
Guitar
模型:
attr_accessible :photos_attributes
in my case I need create a document. 就我而言,我需要创建一个文档。
# config/initializer/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
This document is necessary create in config > initializers. 必须在config>初始值设定项中创建此文档。 If your SO is windows, you need add this line in config/environments/development.rb:
如果您的SO是Windows,则需要在config / environments / development.rb中添加以下行:
Paperclip.options[:comand_path] = "/ImageMagick-6.8.9-Q16/"
Paperclip.options [:comand_path] =“ /ImageMagick-6.8.9-Q16/”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.