2016-09-11 18 views
0

非常にシンプルな管理パネルを作成しました。ここで管理者は記事を編集して破壊することができます。イメージを編集できません

編集は予想通り、私が好きな...私はテキストを編集することができます動作しませんが、写真は がどのように私はこの問題を解決することができます...私は変更しても...同じまま? ご協力いただきありがとうございます!

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 

これは、これは私のモデル

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 

EDITであなたの助けのための私の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 

感謝:)

EDIT 1

です2

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

を参照してください。進行状況モデルで 'accepts_nested_attributes_for:child'を使用しましたか? – vitomd

+0

はい、私の編集を確認してください:) –

+0

更新メソッドで正しいパラメータを受け取っているかどうかを確認してください。メソッドの最初の行に 'puts(" params ________ ")+ params.to_json'を追加し、いくつかのレコードを編集して更新しようとしたら、progress_attachmentsを確認してください。また、progress_attachments添付ファイルモデルを表示できますか?たぶん、別のフィールドや何かを再構築するいくつかの検証があります。 – vitomd

答えて

0

progressモデルでは、progress_attachmentsのネストされた属性を受け入れる必要があります。

class Progress 
    accepts_nested_attributes_for :progress_attachments 
end 

は進行モデルとイメージ(または添付ファイル)モデルとの間の関係は何ですか?accepts_nested_attributes_for

+0

はい私はこれを持っていた、チェックアウト私の編集:) –

関連する問題