Rails 4:使用回形针上传多张图片

詹姆斯·奥斯本

我想将多张图片上传到“位置”模型。我称图像模型为“资产”。一个地点有多个资产。我还使用回形针来处理上传内容,并使用nested_form来选择多个资产。

奇怪的是,位置哈希看起来正确地传递了变量,但是资产模型似乎没有选择它们。任何帮助将是巨大的!

位置模型

class Location < ActiveRecord::Base

  has_many :location_post
  has_many :posts, :through => :location_post  
  has_many :assets, dependent: :destroy

  attr_accessor :asset, :assets_attributes
  accepts_nested_attributes_for :assets, :allow_destroy => true 
end

资产模型

class Asset < ActiveRecord::Base

   belongs_to :location

   has_attached_file :asset,
                    :styles => {
                      :blurred => "600x300^",:large => "600x600>", :medium => "250x250^" , :thumb => "100x100^"},
                      #:source_file_options =>  {:all => '-rotate "-90>"'},
                      :convert_options => {
                      :all => '-auto-orient', :blurred => "-blur 0x6 +repage -resize 600x300^" 
                        },
                      :storage => :s3,
                      :s3_credentials => "#{Rails.root}/config/s3.yml",
                      :bucket => "[bucketname]",
                      :path => "/:style/:id/:filename"    

validates_attachment_content_type :asset, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

end

位置控制器

class LocationsController < ApplicationController

...

  def new
    @location = Location.new
    @location.assets.build
    @georesult = Geocoder.search(params[:query])
  end

  def create

    @location = Location.find_or_create_by(name: location_params[:name])


    respond_to do |format|
     if @location.save
       format.html { redirect_to @location, notice: ' <borat voice> Great success! </borat voice>' }
       format.json { render :show, status: :created, location: @location }
     else
       format.html { render :new }
       format.json { render json: @location.errors, status: :unprocessable_entity }
     end
   end
  end

  # PATCH/PUT /locations/1
  # PATCH/PUT /locations/1.json
  def update
    respond_to do |format|
     if @location.update(location_params)
      format.html { redirect_to @location, notice: 'Location was successfully updated.'  }
      format.json { render :show, status: :ok, location: @location }
     else
      format.html { render :edit }
      format.json { render json: @location.errors, status: :unprocessable_entity }
     end
    end
  end

 ...

 private
    # Use callbacks to share common setup or constraints between actions.
 def location_params
      params[:location].permit(:name, :notes, :longitude, :country, :latitude, :query, assets_attributes: [ :asset, :asset_content_type, :asset_file_name, :tempfile, :asset_file_size, :asset_updated_at, :_destroy])
    end
 end

表格检视

<%= nested_form_for(@location, :html=> {:multipart => true}) do |f| %>

...

  <%= f.fields_for :assets do |a| %>
    <%= a.file_field :asset %>
    <%= a.link_to_remove "Remove this image" %>
  <% end %>
<%= f.link_to_add "Add an image", :assets %>

...

    <%= f.submit "Submit", :class => "btn btn-success submit_location" %>

<% end %>

日志输出

Processing by LocationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"n4spoLjq4B3sZSJjqsGFRVjkseOwGgvquAHATBRG1Nk=", "location"=>{"name"=>"York", "notes"=>"", "lat
itude"=>"53.96230079999999", "longitude"=>"-1.0818844", "country"=>"", "assets_attributes"=>{"0"=>{"asset"=>#<ActionDispatch::Http::UploadedFile
:0x007ff739b7bb68 @tempfile=#<Tempfile:/var/folders/sc/gps8hkgj7yg31j81gpnfg9h00000gn/T/RackMultipart20140706-43312-kdpghs>, @original_filename=
"78509.max1024.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"location[assets_attributes][0][asset]\"; filen
ame=\"78509.max1024.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}}}, "commit"=>"Submit", "id"=>"240"}
  User Load (0.6ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Location Load (0.4ms)  SELECT  "locations".* FROM "locations"  WHERE "locations"."id" = $1 LIMIT 1  [["id", 240]]
   (0.2ms)  BEGIN
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/locations/240
Completed 302 Found in 9ms (ActiveRecord: 1.6ms)
基蒂·索拉特(Kirti Thorat)

我在您的代码中看到了几个问题:

首先,您需要从Location模型中删除以下行

attr_accessor :asset, :assets_attributes

作为其制造assetasset_attributes虚拟属性,这就是为什么它们不保存在数据库中的原因。另外,您不需要模型中的asset属性,Location因为模型已经照顾了它Asset

然后,location_params按照@Pavan的建议更新

def location_params
  ## Use `require` method
  params.require(:location).permit(:name, :notes, :longitude, :country, :latitude, :query, assets_attributes: [ :asset, :asset_content_type, :asset_file_name, :tempfile, :asset_file_size, :asset_updated_at, :_destroy])
end

接下来,如下更新create操作,以确保“位置”在名称上是唯一的:

def create
  @location = Location.find_by(name: location_params[:name])
  unless @location
    @location = Location.new(location_params)
  end 
  respond_to do |format|
    if @location.save
      format.html { redirect_to @location, notice: ' <borat voice> Great success! </borat voice>' }
      format.json { render :show, status: :created, location: @location }
    else
      format.html { render :new }
      format.json { render json: @location.errors, status: :unprocessable_entity }
    end
  end
end 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails 4:使用回形针上传多张图片

来自分类Dev

Rails使用回形针上传图片并进行设计

来自分类Dev

使用回形针上传base64图像-Rails 4

来自分类Dev

用回形针上传多张图片?

来自分类Dev

如何使用回形针在Rails 4中上传多个图像

来自分类Dev

如何使用回形针在另一个Ruby on Rails应用程序上上传图片?

来自分类Dev

尝试使用回形针上传图像时,Rails出现错误

来自分类Dev

Rails 4 SSL错误停止使用回形针直接进行s3上传

来自分类Dev

使用回形针和活动管理员上传多张图片

来自分类Dev

如何使用Rails中的ruby用回形针即时预览上传的图像

来自分类Dev

使用回形针和simple_form将文件上传到Rails 4.0

来自分类Dev

在Rails 4.2中使用回形针上传非图像文件时出错

来自分类Dev

使用回形针和simple_form将文件上传到Rails 4.0

来自分类Dev

使用回形针在Rails应用程序中上传照片时,显示内容类型无效

来自分类Dev

Rails 4-回形针-使用DATA URI上传图像

来自分类Dev

Rails 4-回形针-使用DATA URI上传图像

来自分类Dev

无法在Rails 4中使用回形针保存图像属性

来自分类Dev

Rails 4回形针图片未上传/未显示(仅missing.png)

来自分类Dev

Rails 4 Angularjs回形针如何上传文件

来自分类Dev

Rails 4 Angularjs回形针如何上传文件

来自分类Dev

使用回形针通过我的 rails-app 上传的一些图像未出现在 heroku 上

来自分类Dev

Rails回形针图片上传在移动设备上不起作用

来自分类Dev

使用回形针上传图片时出现“未知属性:头像”?

来自分类Dev

Rails 4 +回形针:制作默认的个人资料图片

来自分类Dev

Rails 4回形针FactoryGirl文件上传

来自分类Dev

无法在Rails和回形针上使用ruby上传zip文件

来自分类Dev

使用回形针上传图片时出现未定义的“图片”错误?

来自分类Dev

使用回形针保存照片时,Rails应用程序“无法分配内存”

来自分类Dev

如何使用回形针,Angular js,ng-file-upload在rails中创建可选附件

Related 相关文章

  1. 1

    Rails 4:使用回形针上传多张图片

  2. 2

    Rails使用回形针上传图片并进行设计

  3. 3

    使用回形针上传base64图像-Rails 4

  4. 4

    用回形针上传多张图片?

  5. 5

    如何使用回形针在Rails 4中上传多个图像

  6. 6

    如何使用回形针在另一个Ruby on Rails应用程序上上传图片?

  7. 7

    尝试使用回形针上传图像时,Rails出现错误

  8. 8

    Rails 4 SSL错误停止使用回形针直接进行s3上传

  9. 9

    使用回形针和活动管理员上传多张图片

  10. 10

    如何使用Rails中的ruby用回形针即时预览上传的图像

  11. 11

    使用回形针和simple_form将文件上传到Rails 4.0

  12. 12

    在Rails 4.2中使用回形针上传非图像文件时出错

  13. 13

    使用回形针和simple_form将文件上传到Rails 4.0

  14. 14

    使用回形针在Rails应用程序中上传照片时,显示内容类型无效

  15. 15

    Rails 4-回形针-使用DATA URI上传图像

  16. 16

    Rails 4-回形针-使用DATA URI上传图像

  17. 17

    无法在Rails 4中使用回形针保存图像属性

  18. 18

    Rails 4回形针图片未上传/未显示(仅missing.png)

  19. 19

    Rails 4 Angularjs回形针如何上传文件

  20. 20

    Rails 4 Angularjs回形针如何上传文件

  21. 21

    使用回形针通过我的 rails-app 上传的一些图像未出现在 heroku 上

  22. 22

    Rails回形针图片上传在移动设备上不起作用

  23. 23

    使用回形针上传图片时出现“未知属性:头像”?

  24. 24

    Rails 4 +回形针:制作默认的个人资料图片

  25. 25

    Rails 4回形针FactoryGirl文件上传

  26. 26

    无法在Rails和回形针上使用ruby上传zip文件

  27. 27

    使用回形针上传图片时出现未定义的“图片”错误?

  28. 28

    使用回形针保存照片时,Rails应用程序“无法分配内存”

  29. 29

    如何使用回形针,Angular js,ng-file-upload在rails中创建可选附件

热门标签

归档