如何使多态模型起作用?

时间:2018-08-25 22:43:18

标签: ruby-on-rails polymorphism

我有两个模型User和Image作为多态关联,因为我希望我的图像模型可以在其他模型中重用。

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

然后我将Image设置为多态:true,并使用carrierwave gem来创建上载程序`mount_uploader mount_uploader:image,在Image model:image中创建ImageUploader

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

在我的视图表单中,我有下一个代码:

<%= 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 %>

但是当我想更新用户例如尝试上传图像时,我得到了下一个图像:

Here is what I have as response

我无法保存上传图片。这是为什么?我希望在db中插入一个片段,但不会发生,并且在db中没有附加的图像。

1 个答案:

答案 0 :(得分:0)

由于要添加多个图像,因此请将表单更改为:

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

在控制器中:

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

让我知道编辑后是否仍然存在问题:)