【问题标题】:Pass multiple images with nested attributes in polymorphic association在多态关联中传递具有嵌套属性的多个图像
【发布时间】:2017-01-27 05:14:09
【问题描述】:

我正在尝试使用 accept_nested_attributes_for 和多态关联传递多个图像。但是收到此错误no implicit conversion of Symbol into Integer。虽然我知道我已经完成了所有设置,但我仍然不知道我错过了什么。我已经使用carrier wave 进行图片上传。

用户.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

users/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)

【问题讨论】:

  • 嗨。如果您查看日志(在控制台窗口或log/development.log 中并找到与该错误消息匹配的堆栈跟踪并将其复制/粘贴到您的问题中,这将有助于我们为您提供帮助。这将有助于我们了解找出导致该错误的哪一行(不仅在您的代码中,而且在载波中)。
  • 但只是一个小测验也告诉我这可能不太正确:f.fields_for :images_attributes 应该是:f.fields_for :images(在表格中。在许可/要求中,否则使用是正确的images_attributes)
  • 好的,我正在向您展示我的 development.log
  • Taryn East- 我试过用 f.fields_for :images 不起作用
  • @AniketShivamTiwari:请让我知道错误。并且不要忘记将 id 添加到属性中 images_attributes: [:id, :name,:image,:user_id ]

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


【解决方案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


users/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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多