无法编辑图像

时间:2016-09-11 17:05:53

标签: ruby-on-rails ruby-on-rails-4

我创建了一个非常简单的管理面板,管理员可以在其中创建编辑和销毁文章。

编辑没有按预期工作......我可以根据需要编辑文字,但图片保持不变......即使我改变了...... 我怎样才能解决这个问题? 谢谢你的帮助!

  class Admin::ProgressesController < Admin::ApplicationController

    if Rails.env.production?
      http_basic_authenticate_with :name => ENV["ADMIN_NAME"],:password => ENV["ADMIN_PASSWORD"]
    else
      http_basic_authenticate_with :name => "admin", :password => "password"
    end

    def index
      @progresses = Progress.all
    end

    def new
      @progress = Progress.new
    end

    def show
      @progress = Progress.find(params[:id])

    end

    def create
       @progress = Progress.new(progress_params)

       respond_to do |format|
         if @progress.save
           unless params[:progress_attachments].nil?
             params[:progress_attachments]['image'].each do |a|
               @progress_attachment = @progress.progress_attachments.create!(:image => a)
             end
           end
           format.html { redirect_to admin_progresses_path, notice: 'Progress was successfully created.' }
         else
           format.html { render action: 'new' }
         end
       end

     end

     def edit
       @progress = Progress.find(params[:id])
     end

     def destroy
        @progress = Progress.find(params[:id])
        @progress.destroy
        flash[:success] = "Article was successfully deleted"
        redirect_to admin_progresses_path
     end

#EDIT: Added the  `puts ("params________")+params.to_json`
    def update
      puts ("params________")+params.to_json
      @progress = Progress.find(params[:id])
        if @progress.update(params[:progress].permit(:title, :content, :date, :main_image, progress_attachments_attributes: [:id, :progress_id, :image] ))
          flash[:success] = "Article was successfully updated"
          redirect_to admin_progresses_path

        else
      render 'edit'
        end
        end

     private

     def progress_params
        params.require(:progress).permit(:title, :content, :date, :main_image, progress_attachments_attributes: [:id, :progress_id, :image])
     end

  end

这是我的index.html.slim

.container
  .row
    h1 Pepito's Dashboard
    br
    = link_to "New article", new_admin_progress_path, class:'btn btn-success'
    br
    br

    table.board
      thead
        tr
          th
            .table-title Title
          th
            .table-date Date
          th
            .table-content Content
          th
            .table-actions Actions
    tbody
    hr.bold-line
      .dashboard
        - @progresses.each do |progress|
          .row 
            .col-xs-2
              h4
              = progress.title.capitalize
            .col-xs-2
              h4
              = progress.date
            .col-xs-5
              h4  
              = truncate(progress.content, length: 90)
            .col-xs-1
              = link_to 'View', progress_path(progress), class:'btn btn-success'
            .col-xs-1
              = link_to 'Edit', edit_admin_progress_path(progress), class:'btn btn-primary'
            .col-xs-1
              = link_to 'Destroy', admin_progress_path(progress), class:'btn btn-danger', method: :delete, data: {confirm: "Are you sure Pepito?"}
          hr

感谢您的帮助:)

编辑1

这是我的模特

class Progress < ActiveRecord::Base
  default_scope ->{order(created_at: :DESC)}
  has_many :progress_attachments
  accepts_nested_attributes_for :progress_attachments, :allow_destroy => true
  mount_uploader :main_image, ImageUploader
  validates :main_image,   presence: true
  validates :title,   presence: true
  validates :content,  presence: true
  validates :date,    presence: true

end

编辑2

class ProgressAttachment < ActiveRecord::Base
  mount_uploader :image, ImageUploader
  belongs_to :progress
  validates :image, presence: true  
end

1 个答案:

答案 0 :(得分:0)

您需要在progress模型中接受class Progress accepts_nested_attributes_for :progress_attachments end 的嵌套属性。

const int n_indices = 1000000;

参考accepts_nested_attributes_for