1

我想通过accept_nested_attributes_for和多态关联传递多个图像。但得到这个错误no implicit conversion of Symbol into Integer。尽管我知道我已经完成了所有的设置,但我不知道我错过了什么。并且我使用carrierwave进行图像上传。传递多个图像嵌套属性和多态关联

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

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 

个用户/ 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 %> 

登录

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) 
+0

嗨。如果您查看日志(无论是在控制台窗口还是在“log/development.log”中),并找到与该错误消息相匹配的堆栈跟踪并将其复制/粘贴到您的问题中,它会帮助我们帮助您。将帮助我们找出哪条线(不仅在你的代码中,而且在carrierwave中)导致了这个错误 –

+0

但是,只是一个快速的squiz也告诉我,这可能不是很正确:'f.fields_for:images_attributes'它应该可能是:'f.fields_for:images'(在许可证/要求的形式中,使用'images_attributes'是正确的) –

+0

好吧,我向你展示我的发展.log –

回答

1

我已经找到了解决办法

users_controller.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 


用户/ 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 %>