编辑帖子时,我的照片字段没有保存选择

时间:2019-02-06 11:23:05

标签: ruby-on-rails ruby postgresql

当我在后台编辑帖子时出现问题。我创建帖子时,所有belong_to表都没有保存信息。

我在网络上找不到解决方案。我不是实验开发者,所以我现在不在必须寻找的地方。

我的upload_file:

require "image_processing/mini_magick"

class ImageUploader < Shrine
 plugin :processing # allows hooking into promoting
 plugin :versions   # enable Shrine to handle a hash of files
 plugin :delete_raw # delete processed files after uploading

 process(:store) do |io, context|
   original = io.download
   pipeline = ImageProcessing::MiniMagick.source(original)

   size_200 = pipeline.resize_to_limit!(200, 200)
   size_300 = pipeline.resize_to_limit!(300, 300)

   original.close!

   { original: io, moyen: size_300, small: size_200 }
 end
end

我的模特:

has_one :photo, as: :attachable
  accepts_nested_attributes_for :photo

我的控制器:

# frozen_string_literal: true

class Admin::ActualitiesController < Admin::AdminController
  before_action :set_actuality, only: %i[show edit update destroy]

  def index
    @actualities = Actuality.order('id DESC').page(params[:page]).per(10)
  end

  def new
    @actuality = Actuality.new
    @actuality.build_photo
  end

  def edit
  end

  def show; end

  def create
    @actuality = Actuality.new(actuality_params)

    if @actuality.save
      redirect_to :show, notice: ''
    else
      render :new, notice: ''
    end
  end

  def update
    if @actuality.update(actuality_params)
      redirect_to :show, notice: ''
    else
      render :edit
    end
  end

  def destroy
    @actuality.destroy
    redirect_to admin_actualities_path, notice: ''
  end

  private

  def set_actuality
    @actuality = Actuality.find_by(id: params[:id])
  end

  def actuality_params
    params.require(:actuality).permit(:title, :body, :apercu, :meeting, photo_attributes: [:photo])
  end
end

以我的形式:

<%= f.simple_fields_for :photo do |ff| %>
    <%= ff.input :photo, as: :hidden %>
    <%= ff.input :photo, as: :file %>
<% end %>

如果您有任何解决方案,我在这里! 谢谢!

0 个答案:

没有答案