【问题标题】:Multiple Uploads Rails Paperclip not Saving多个上传 Rails 回形针不保存
【发布时间】:2013-10-20 13:41:08
【问题描述】:

我有一个 rails 型号“Guitar”和一个 rails 型号“Photo”。我希望能够附加多张照片,所以我观看了 Paperclip 的 railscasts,阅读了文档等。我过去曾使用 Paperclip 附加一张照片,但这次我想做多张照片。我更新模型没有问题,当我附加示例照片时它不会出错,但是当我将控制台添加到 Photos.all 时,什么也没有回来。我的最终目标是与“Amps”模型建立多态关联,但我真的只是想让“吉他”首先工作。

我在 Gemfile 中添加了回形针,并且我已经安装了包,重新启动了服务器等。我想我错过了一些愚蠢的东西,而且我是 Rails 新手,所以请温柔...

吉他.rb

class Guitar < ActiveRecord::Base
  belongs_to :user
  has_many :photos

  accepts_nested_attributes_for :photos
end

照片.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

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

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

吉他_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 %>

【问题讨论】:

  • 您是否遇到任何错误?尝试在保存之前添加中止,看看您是否正在获取文件。
  • 没有错误。它只是不会将照片保存到模型中 - 我可以控制台并执行 Photos.all,但似乎没有任何东西被保存。 P2.0.0-p247 :001 > Photo.all Photo Load (1.6ms) SELECT "photos".* FROM "photos" => #<:relation>
  • 我确实在日志中注意到了这一点:未经许可的参数:photos_attributes
  • 如果您使用的是 Rails 4,您应该更改标签以反映您正在使用的标签:)
  • 我刚做了,真的很抱歉。

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip


【解决方案1】:

您遇到了批量分配保护,导致照片无法保存。将此行添加到您的 Guitar 模型中:

attr_accessible :photos_attributes

【讨论】:

  • attr_accessible 从 Rails 中提取到 gem 中。请为 params(strong_parameters) 使用新的推荐保护模型或将 protected_attributes 添加到您的 Gemfile 以使用旧的。我在 Rails 4 上。
  • 这个问题用 ror-3 标记。强参数是 Rails 4 中的新功能,attr_accessible 在 Rails 3 中仍然是正确的方法。
  • 对不起,我用 RoR3 标记了这个问题,意思是点击 RoR4。我在 4 点。
【解决方案2】:
 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

【讨论】:

  • 这在 Rails 3 中不起作用(这个问题用 rails 3 标记)。
  • 那家伙说他正在使用 Rails 4 --> “我在 Rails 4 上。”通过 momchenr
  • 糟糕,他确实做到了。我的错。
【解决方案3】:

在我的情况下,我需要创建一个文档。

# config/initializer/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

这个文件是必须在 config > initializers 中创建的。如果你的 SO 是 windows,你需要在 config/environments/development.rb 中添加这一行:

Paperclip.options[:comand_path] = "/ImageMagick-6.8.9-Q16/"

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 2016-03-31
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多