简体   繁体   English

在多态关联中传递具有嵌套属性的多个图像

[英]Pass multiple images with nested attributes in polymorphic association

I am trying to pass multiple images with accepts_nested_attributes_for and polymorphic association.我正在尝试使用 accepts_nested_attributes_for 和多态关联传递多个图像。 But getting this error no implicit conversion of Symbol into Integer .Though I know I have done all the setup still I don't know what am I missing.但是得到这个错误no implicit conversion of Symbol into Integer虽然我知道我已经完成了所有的设置,但我不知道我错过了什么。 And I have used carrier wave for image uploading.我已经使用carrier wave进行图像上传。

User.rb用户名

class User < ApplicationRecord  
  has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id'  ,dependent: :destroy
  accepts_nested_attributes_for :images 
     
  validates :first_name,presence: true
end

Image.rb图像文件

class Image < ApplicationRecord
  before_destroy :remember_id
  after_destroy :remove_id_directory

  mount_uploader :image, ImageUploader 
  belongs_to :object,polymorphic: true

  validates :name,presence: true

  protected

  def remember_id
    @id = id
  end

  def remove_id_directory
    FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
  end
end

users_controller.rb用户控制器.rb

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def new
    @user = User.new
    @user.images.build
  end

  def create
    @user = User.new 
     @user.images.build(user_params)
    if @user.save
      redirect_to users_path 
    else
      render :new
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    redirect_to users_path
  end

  private
     
  def user_params
    params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
  end
end

users/new.html.erb用户/new.html.erb

<%= form_for @user,html: {multipart: :true} do |f| %>   
  <%= f.text_field :first_name %>

  <%= f.fields_for :images_attributes do |images_fields| %>
    Nama  : <%= images_fields.text_field :name %>
    Image: <%= images_fields.file_field :image,:multiple => true %>

  <% end %>
  <%=f.submit "Submit" %>
<% end %>

Log日志

   Started POST "/users" for 127.0.0.1 at 2017-01-27 11:20:29 +0530
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"wpb1cqph+SAucEfeb0isx7DKtsV4PQeyq47xZbz/Ac7cSfoSleBXynNJiT+kNni5OaX/DqNhR+h1Xvli2QyBbg==", "user"=>{"first_name"=>"adasd", "images_attributes"=>{"0"=>{"name"=>"asdasd", "image"=>[#<ActionDispatch::Http::UploadedFile:0x00000003819838 @tempfile=#<Tempfile:/tmp/RackMultipart20170127-3305-16cl68l.png>, @original_filename="1.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][image][]\"; filename=\"1.png\"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x000000038197e8 @tempfile=#<Tempfile:/tmp/RackMultipart20170127-3305-8o6vn7.png>, @original_filename="27_dec.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][image][]\"; filename=\"27_dec.png\"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x000000038196a8 @tempfile=#<Tempfile:/tmp/RackMultipart20170127-3305-1j7hl1r.png>, @original_filename="Screenshot from 2017-01-13 16:52:49.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][image][]\"; filename=\"Screenshot from 2017-01-13 16:52:49.png\"\r\nContent-Type: image/png\r\n">]}}}, "commit"=>"Submit"}
Unpermitted parameter: image
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)

I have found the solution我找到了解决方案

users_controller.rb用户控制器.rb

def create
    @user = User.new(user_params)  
    if @user.save
        params[:images_attributes]['image'].each do |a|

          @image_attachment = @user.images.create!(:image => a,:name=>  params[:images_attributes][:name].join)

       end

        redirect_to users_path

    else
      render :new
    end
  end


users/new.html.erb用户/new.html.erb

<%= form_for @user,html: {multipart: :true} do |f| %>

    <%= f.text_field :first_name %> 

    <%= f.fields_for :images do |images_fields| %>
    Name  : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
    Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>

    <% end %>
       <%=f.submit "Submit" %>

<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM