使用嵌套属性和多态关联传递多个图像

时间:2017-01-27 05:14:10

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5 polymorphic-associations

我正在尝试使用accepts_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 个答案:

没有答案