Ruby on rails Rails 4:使用回形针上传多幅图像

Ruby on rails Rails 4:使用回形针上传多幅图像,ruby-on-rails,paperclip,nested-forms,image-uploading,Ruby On Rails,Paperclip,Nested Forms,Image Uploading,我希望上传多张图片到我的“位置”模型。我将图像模型称为“资产”。一个位置有多个资产。我还使用回形针来处理上传和嵌套表单,以允许选择多个资产 奇怪的是,locations散列看起来正确地传递了变量,但资产模型似乎没有拾取它们。任何帮助都会很好 位置模型 类位置:位置\u帖子 有很多:资产,依赖::销毁 属性访问器:资产,:资产属性 接受\u嵌套的\u属性\u for:assets,:allow\u destroy=>true 结束 资产模型 class资产{ :fuzzle=>“600x300^

我希望上传多张图片到我的“位置”模型。我将图像模型称为“资产”。一个位置有多个资产。我还使用回形针来处理上传和嵌套表单,以允许选择多个资产

奇怪的是,locations散列看起来正确地传递了变量,但资产模型似乎没有拾取它们。任何帮助都会很好

位置模型

类位置:位置\u帖子
有很多:资产,依赖::销毁
属性访问器:资产,:资产属性
接受\u嵌套的\u属性\u for:assets,:allow\u destroy=>true
结束
资产模型

class资产{
:fuzzle=>“600x300^”、:large=>“600x600>”、:medium=>“250x250^”、:thumb=>“100x100^”,
#:source_file_options=>{:all=>'-rotate“-90>”},
:convert_options=>{
:all=>'-自动定向',:fuzzle=>“-模糊0x6+重新分页-调整大小600x300^”
},
:storage=>:s3,
:s3_credentials=>“#{Rails.root}/config/s3.yml”,
:bucket=>“[bucketname]”,
:path=>“/:style/:id/:filename”
验证附件内容类型:资产、内容类型=>[“image/jpg”、“image/jpeg”、“image/png”、“image/gif”]
结束
位置控制器

类位置控制器
窗体视图

{:multipart=>true})do | f |%>
...
...
“btn btn成功提交位置”%>
日志输出

由LocationsController处理#更新为HTML
参数:{“utf8”=>“✓", "真实性令牌“=>”N4SPOLJQ4B3SZSJQSGFRVJKSEOWGGVQUAHATBRG1NK=“,”位置“=>{”名称“=>”约克“,”注释“=>”,“lat”
纬度“=>”53.9623007999999“,”经度“=>”-1.0818844“,”国家“=>”,”资产属性“=>{”0“=>{”资产“=>,”销毁“=>”假“}}>,”提交“=>”提交“,”id“=>”240”}
用户加载(0.6ms)从“用户”中选择“用户”。*其中“用户”。“id”=1“用户”的订单。“id”ASC限制1
位置加载(0.4ms)选择“位置”。*从“位置”中选择“位置”。“id”=$1限制1[[“id”,240]]
(0.2ms)开始
(0.3ms)提交
重定向到http://localhost:3000/locations/240
9毫秒内完成302次(ActiveRecord:1.6毫秒)

我发现您的代码中有两个问题:

首先,您需要从
Location
model中删除以下行:

attr_accessor :asset, :assets_attributes
它将
资产
资产属性
作为虚拟属性,这就是它们不保存在数据库中的原因。此外,您不需要在
位置
模型中使用
资产
属性,因为它由
资产
模型负责

然后,按照@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 
def创建
@位置=位置。查找方式(名称:位置参数[:名称])
除非@location
@位置=位置。新建(位置参数)
结束
回应待办事项|格式|
如果@location.save
format.html{将_重定向到@location,注意:'非常成功!'
format.json{render:show,status::created,location:@location}
其他的
format.html{render:new}
format.json{render json:@location.errors,status::unprocessable_entity}
结束
结束
结束
尝试使用
的“true”和:name=>“location[assets][assets][]”%>
来处理多个上载


希望它有帮助

尝试将
location\u参数更改为
params.require(:location)。permit(:name,:notes,:longitude,:country,:latitude,:query,assets\u属性:[:asset,:asset\u content\u type,:asset\u file\u name,:tempfile,:asset\u file\u size,:asset\u updated\u at,:\u destroy])/code>您的操作完成了吗?我不认为您正在传递参数或调用已创建对象的保存。@Pavan谢谢,但尝试了此操作,但没有掷骰子。日志显示哈希也被正确构建,但只是没有正确响应。还有其他想法吗?@mus我相信这是一个完整的行动。如果没有通过,它肯定不会在日志中?就“create”函数而言,我只是正常运行它。我将修改上面的帖子以包含这一点。你为什么有这一行
@location=location.find\u或\u create\u by(name:location\u params[:name])
?它是
<%= 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)
attr_accessor :asset, :assets_attributes
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
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