0
votes

I've got two models User and Image as polymorphic association because I want my image model to reuse in other models.

class User < ApplicationRecord

  has_one :cart
  has_many :images, as: :imageable, dependent: :destroy
  accepts_nested_attributes_for :images

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  before_validation :set_name, on: :create
  validates :name, presence: true

  private

  def set_name
    self.name = "person#{rand(1000)}" if self.name.blank?
  end
end

class Image < ApplicationRecord
  mount_uploader :image, ImageUploader
  belongs_to :imageable, polymorphic: true
end

And I made Image polymorphic: true and use carrierwave gem for creating uploader `mount_uploader mount_uploader :image, ImageUploader in Image model:image

class ImageUploader < CarrierWave::Uploader::Base
end
and I permit :image parameters to each model: User and Good,

module Admin
  class UsersController < BaseController

    before_action :set_admin_user, only: [:show, :edit, :update, :destroy]

    def users_list
      @admin_users = User.all.preload(:images).where(admin: true)
    end

    def show
    end

    def edit
    end

    def update
      if @user.images.update(admin_user_params)
        redirect_to admin_users_list_path, notice: 'User was successfully updated'
      else
        flash[:alert] = 'User was not updated'
      end
    end

    def destroy
    end

    private

    def set_admin_user
      @user = User.find(params[:id])
    end

    def admin_user_params
      params.require(:user).permit(:name, :email, images_attributes: [:image])
    end
  end
end

In my view form I've got the next code:

<%= form_for [:admin, @user], html: { multipart: true } do |f| %>
  <%= f.label 'Name', class: 'form-group' %>
  <%= f.text_field :name, class: 'form-control' %>
  <%= f.fields_for :images_attributes do |i| %>
    <%= i.label :image %>
    <%= i.file_field :image %>
  <% end %>
  <%= f.label 'Email', class: 'form-group' %>
  <%= f.text_field :email, class: 'form-control' %>
  <%= f.submit class: 'btn btn-oultline-primary' %>
<% end %>

but when I want to update user for exampletry to upload the image I've got the next:

Here is what I have as response

I can't saveupload my image. Why is that? I expect to have an insert into db but it doesn't happen and in db I've got no attached images.

1

1 Answers

0
votes

Since you are adding multiple images, change your form to:

<%= i.file_field :image, multiple: true, name: "images_attributes[image][]" %>

And in the controller:

def edit
  @image = @user.images.build
end

def update
  if @user.images.update(admin_user_params)
    create_user_images
    redirect_to admin_users_list_path, notice: 'User was successfully updated'
  else
    flash[:alert] = 'User was not updated'
  end
end

private

def admin_user_params
  params.require(:user).permit(:name, :email, images_attributes: [:id, :user_id, :image])
end

def create_user_images
  if params[:images_attributes]
    params[:images_attributes]['image'].each do |i|
      @image = @user.images.create!(:image => i)
    end
  end
end

Let me know if you still have problems after the edits :)