Foreach输入数组参数然后在Rails中为每个元素保存1条记录

时间:2016-03-31 04:11:57

标签: ruby-on-rails arrays carrierwave

我正在制作一张表格,用载波上传多张图片。将数组保存到数据库工作正常,但我想将1个数组元素保存为1条记录,以便以后更容易管理。 我尝试按以下方式执行,但它返回nilClass错误。

所有图片使用相同的描述,名称是图像文件名

之前有人做过这种事:'(

视图/ _form

<%= form_for [:admin, @picture], :html => {multipart: true, :class => 'form-horizontal'} do |f| %>
<div class="box-body">

<div class="form-group">
  <%= f.label :link, :class => 'col-sm-2 control-label' %>
  <div class="col-sm-10">
    <%= f.file_field :link, multiple: true, :class => 'form-control', :id => 'imgInp' %>
  </div>
</div>
<div class="form-group">
  <%= f.label :description, :class => 'control-label col-sm-2' %>
  <div class="col-sm-10">
    <%= f.text_area :description, :class => 'form-control' %>
  </div>
</div>
</div>
<!-- /.box-body -->
  <div class="box-footer">
    <%= link_to admin_pictures_path do %>
    <button class="btn btn-default">Back</button>
<% end %>
<%= f.submit nil, :class => 'btn btn-info pull-right' %>
</div>
<!-- /.box-footer -->    <% end %>

控制器

def create

    if params[:link]
    params[:link].each { |image|
      @picture = Picture.new(name: image.file.filename, description: params[:description], link: image)
      @picture.save
    }
    end
    end
    respond_to do |format|
    if @picture.save
      format.html { redirect_to admin_pictures_path, notice: ' picture was successfully created.' }
      format.json { render :show, status: :created, location: @picture }
else
      format.html { render :new }
      format.json { render json: @picture.errors, status: :unprocessable_entity }
    end
    end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_picture
  @picture = Picture.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white  list through.
def picture_params
  params.require(:picture).permit(:name, :description, :link)
end
end

param [:link] console

Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"2KMy3lPMnf3MqLLHpJAH+Ei40nja+KAJGsx2twyP5L05A95b7rLsZHEFoUSu+CMJQunQ4yUq6kmppdK6I7NQkw==", 
"picture"=>{"album_id"=>"3", "team_id"=>"", "link"=>[#<ActionDispatch::Http::UploadedFile:0x007f9095ea1c08 @tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-1h8i98y.jpg>, 
@original_filename="images.jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"picture[link][]\"; 
filename=\"images.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f9095ea1be0 @tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-y6d6ug.jpg>, @original_filename="images (1).jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"picture[link][]\"; 
filename=\"images (1).jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f9095ea1bb8 @tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-hizrku.jpg>, 
@original_filename="lulu.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture[link][]\"; 
filename=\"lulu.jpg\"\r\nContent-Type: image/jpeg\r\n">], 
"description"=>"abc"}, "commit"=>"Create Picture"}
User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1
params: 
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)

2 个答案:

答案 0 :(得分:1)

这将解决您的问题:

def create
    if params[:picture][:link]
    params[:picture][:link].each { |image|
      @picture = Picture.new(:name => image.original_filename,:description => params[:description])
      @picture.store!(image.tempfile)
      @picture.save
    }
    end
    end
    respond_to do |format|
    if @picture.save
      format.html { redirect_to admin_pictures_path, notice: ' picture was successfully created.' }
      format.json { render :show, status: :created, location: @picture }
else
      format.html { render :new }
      format.json { render json: @picture.errors, status: :unprocessable_entity }
    end
    end
end

答案 1 :(得分:0)

尝试

...
@picture = Picture.new(picture_params)
respond_to do |format|
    if @picture.save...

因此 @ picture.save 不会导致 nil