繁体   English   中英

如何使多态模型起作用?

[英]How can I make my polymorphic model work?

我有两个模型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 %>

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

这是我的回应

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

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

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

让我知道您在编辑后是否还有问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM