Wes*_*zz7 4 model ruby-on-rails image paperclip ruby-on-rails-4
我知道Stackoverflow上有几篇帖子和几个关于这个主题的教程.然而,他们都没有设法解决我的问题,其中大多数也已经过时了.
我正在尝试使用Rails 4中的paperclip gem将多个图像添加到项目中.当我尝试上传它时,我确实看到附加在params中的资产.
它们似乎没有添加到project_paramns但是..
希望有人可以帮助我.
这是我的projects_controller
class ProjectsController < ApplicationController
before_filter :find_project, only: [:show, :edit, :update, :destroy]
def index
@projects = Project.all
end
def show
end
def new
@project = Project.new
end
def create
@project = Project.new(project_params)
@project.save
redirect_to project_path(@project)
end
def edit
end
def update
@project.update(project_params)
redirect_to project_path(@project)
end
def destroy
@project.destroy
redirect_to projects_path
end
protected
def project_params
params.require(:project).permit(:name, :description, :asset)
end
def find_project
@project = Project.find(params[:id])
end
end
Run Code Online (Sandbox Code Playgroud)
我的项目模型
class Project < ActiveRecord::Base
has_many :assets, :dependent => :destroy
validates_associated :assets
validates_presence_of :name, :on => :create, :update => "can't be blank"
validates_presence_of :description, :on => :create, :update => "can't be blank"
accepts_nested_attributes_for :assets
end
Run Code Online (Sandbox Code Playgroud)
我的资产模型
class Asset < ActiveRecord::Base
belongs_to :project
# Paperclip
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" }
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
Run Code Online (Sandbox Code Playgroud)
我的形式部分(抱歉,它在HAML)
= simple_form_for @project do |f|
%ul
- @project.errors.full_messages.each do |error|
%li= error
.row
.small-1.columns
= f.label :name, :class => "left inline"
.small-11.columns
= f.input_field :name
.row
.small-1.columns
= f.label :description, :class => "left inline"
.small-11.columns
= f.input_field :description, as: :text
.row
= f.simple_fields_for :asset do |a|
.small-1.columns
= a.label :image, :class => "left inline"
.small-11.columns
= file_field_tag :image, multiple: true,
.row
.small-9.small-offset-1.columns
= f.submit nil ,:class => "button [radius round]"
Run Code Online (Sandbox Code Playgroud)
请求参数
{"utf8"=>"?", "_method"=>"patch", "authenticity_token"=>"4iK1kUNvKvoJVOKoivz/pcLAe6LY0cUJikQioxa8BIs=", "project"=>{"name"=>"adf", "description"=>"adf", "asset"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007fb808357d80 @tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140607-34739-dvlzt7>, @original_filename="enabling-gzip-compression.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"project[asset][image][]\"; filename=\"enabling-gzip-compression.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fb808357d58 @tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140607-34739-lwkioi>, @original_filename="minimize_http_requests.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"project[asset][image][]\"; filename=\"minimize_http_requests.png\"\r\nContent-Type: image/png\r\n">]}}, "commit"=>"Update Project", "action"=>"update", "controller"=>"projects", "id"=>"11"}
Run Code Online (Sandbox Code Playgroud)
现在我的终端出现了错误:
Unpermitted parameters: asset
Run Code Online (Sandbox Code Playgroud)
编辑:
@ pavan和@kiri thorat的答案的组合帮助我在project_params中显示了一些内容,它现在提供的输出是:
{"name"=>"Test", "description"=>"Test", "assets_attributes"=>{"0"=>{}}}
Run Code Online (Sandbox Code Playgroud)
关于这里发生了什么的任何线索?
在@ kirithorat的最新更新之后,事情的参数方面似乎很好.
{"utf8"=>"?", "_method"=>"patch", "authenticity_token"=>"4iK1kUNvKvoJVOKoivz/pcLAe6LY0cUJikQioxa8BIs=", "project"=>{"name"=>"Test", "description"=>"Test", "assets_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fcd383c9a08 @tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140610-36517-7ek1oq>, @original_filename="enabling-gzip-compression.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"project[assets_attributes][0][image]\"; filename=\"enabling-gzip-compression.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Update Project", "action"=>"update", "controller"=>"projects", "id"=>"13"}
Run Code Online (Sandbox Code Playgroud)
但资产仍然没有被保存.
实施@ Valikiliy的建议后更新
{"utf8"=>"?", "_method"=>"patch", "authenticity_token"=>"4iK1kUNvKvoJVOKoivz/pcLAe6LY0cUJikQioxa8BIs=", "project"=>{"name"=>"Test", "description"=>"Test", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fcd3a08d0b8 @tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140610-36517-rgy95n>, @original_filename="minimize_http_requests.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"project[image]\"; filename=\"minimize_http_requests.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Update Project", "action"=>"update", "controller"=>"projects", "id"=>"16"}
Run Code Online (Sandbox Code Playgroud)
更新:根据要求添加新的表单代码
= simple_form_for(@project, :html => { :multipart => true }) do |f|
%ul
- @project.errors.full_messages.each do |error|
%li= error
.row
.small-1.columns
= f.label :name, :class => "left inline"
.small-11.columns
= f.text_field :name
.row
.small-1.columns
= f.label :description, :class => "left inline"
.small-11.columns
= f.text_area :description
.row
= f.simple_fields_for :assets do |a|
.small-1.columns
= a.label :image, :class => "left inline"
.small-11.columns
- if a.object.new_record?
= a.input :image, as: :file
- else
= image_tag a.object.image.url(:thumb)
= a.input_field '_destroy', as: :boolean
.row
.small-9.small-offset-1.columns
= f.submit nil ,:class => "button [radius round]"
Run Code Online (Sandbox Code Playgroud)
更新
def project_params
params.require(:project).permit(:name, :description, images: [], assets_attributes: [:_destroy, :id, :image])
end
def find_project
@project = Project.find(params[:id])
@project.assets.build if %w[new edit].include?(action_name)
end
Run Code Online (Sandbox Code Playgroud)
更新:添加了型号代码
class Project < ActiveRecord::Base
has_many :assets, :dependent => :destroy
validates_presence_of :name, :on => :create, :update => "can't be blank"
validates_presence_of :description, :on => :create, :update => "can't be blank"
accepts_nested_attributes_for :assets, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
Vak*_*liy 17
要在上面的例子中保存多个文件,不需要添加multiple:true选项,它会在保存时导致错误,一个文件 - 一个记录资产表.
要解决此问题,您可以添加多个文件字段,如下所示:add into controller:
def new
@project = Project.new
3.times{ @project.assets.build }
end
def edit
3.times{ @project.assets.build }
end
Run Code Online (Sandbox Code Playgroud)
对于列表
.permit(..., assets_attributes: [ :_destroy, :id, :image ])
Run Code Online (Sandbox Code Playgroud)
并在这样看来:
= f.simple_fields_for :assets do |a|
.small-1.columns
= a.label :image, :class => "left inline"
- if a.object.new_record?
.small-11.columns
= a.file_field :image
- else
.small-11.columns
= image_tag a.object.image.url(:thumb)
= a.input_field '_destroy', as: :boolean
Run Code Online (Sandbox Code Playgroud)
如果您尝试发送多个这样的图像:
# view
a.file_field :image, multiple: true
# controller
.permit(..., assets_attributes: [ :_destroy, :id, image: [] ])
Run Code Online (Sandbox Code Playgroud)
这将导致错误,因为Asset模型不知道如何处理图像数组.
要同时保存多个文件,您需要使用自己的处理程序,例如:
model:添加images=方法
def images=(files = [])
assets.create(image: f)
end
Run Code Online (Sandbox Code Playgroud)
controller:移出images: []assets_attributes
.permit(..., images: [], assets_attributes: [ :_destroy, :id ])
Run Code Online (Sandbox Code Playgroud)
view:删除fields_for和嵌套属性
.row
.small-1.columns
=f.file_field :images, multiple: true
.row
= f.simple_fields_for :assets do |a|
.small-1.columns
= a.label :image, :class => "left inline"
....
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11521 次 |
| 最近记录: |