Ruby on rails 多次上传Rails回形针未保存
我有一个rails模型“吉他”和一个rails模型“照片”。我想能够附加多张照片,所以我看了railscasts的回形针,阅读了文档,等等。我过去用回形针附加一张照片,但这次我想做多次。我在更新模型时没有问题,当我附加示例照片时也不会出错,但是当我将rails控制台添加到照片时,什么都不会返回。我的最终目标是与“Amps”模型建立多态关联,但我真的只是想先让“吉他”工作起来 我已经在Gemfile中添加了回形针,我已经安装了包,重新启动了服务器,等等。我想我遗漏了一些愚蠢的东西,我是rails的新手,所以请温柔点 吉他.rbRuby on rails 多次上传Rails回形针未保存,ruby-on-rails,ruby,ruby-on-rails-4,paperclip,Ruby On Rails,Ruby,Ruby On Rails 4,Paperclip,我有一个rails模型“吉他”和一个rails模型“照片”。我想能够附加多张照片,所以我看了railscasts的回形针,阅读了文档,等等。我过去用回形针附加一张照片,但这次我想做多次。我在更新模型时没有问题,当我附加示例照片时也不会出错,但是当我将rails控制台添加到照片时,什么都不会返回。我的最终目标是与“Amps”模型建立多态关联,但我真的只是想先让“吉他”工作起来 我已经在Gemfile中添加了回形针,我已经安装了包,重新启动了服务器,等等。我想我遗漏了一些愚蠢的东西,我是rails的
class Guitar < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos
end
class吉他
photo.rb
class Photo < ActiveRecord::Base
belongs_to :guitars
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>',
large: '600x600#' }
end
class-Photo”,
正方形:“200x200#”,
中等:“300x300>”,
大型:“600x600#”
结束
吉他控制器.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
class GuitarController
guitars 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 %>
制作:
型号:
年份:
颜色:
序列号:
价格:
条件:
种类:
Bodykind:
烦恼:
一个所有者:
用户:
|
吉他_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 %>
true})do | f |%>
禁止保存此吉他:
您遇到了阻止保存照片的批量分配保护。将此行添加到您的吉他
型号:
attr_accessible :photos_attributes
在我的情况下,我需要创建一个文档
# config/initializer/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
在配置>初始值设定项中创建此文档是必需的。如果您的SO是windows,则需要在config/environments/development.rb中添加此行:
回形针。选项[:comand_path]=“ImageMagick-6.8.9-Q16/”
你有什么错误吗?尝试在保存之前添加一个中止,看看是否正在获取文件。没有错误。它只是不会将照片保存到模型中-我可以在控制台中进行操作并拍摄照片。P2.0.0-p247:001>照片。所有照片加载(1.6ms)选择“照片”。*从“照片”=>#我在日志中注意到了这一点:未授权参数:照片属性如果您使用的是Rails 4,您应该更改标记以反映您正在使用的:)我刚才做了,我真的很抱歉。
attr\u accessible
从Rails中提取到一个gem中。请为参数(强参数)使用新的推荐保护模型,或将受保护的属性添加到GEM文件中以使用旧的保护模型。我在Rails 4上。这个问题用ror-3标记。强参数在Rails 4中是新的,在Rails 3中,attr\u accessible
仍然是正确的方法。对不起,我用RoR3标记了这个问题,意在单击RoR4。我在4上。这在Rails 3上不起作用(这个问题被标记为Rails 3)。这家伙说他在使用Rails 4-->“我在Rails 4上。”momchenrOops说,他确实这样做了。我的错。
# config/initializer/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end