2016-09-01 3 views
0

안녕하세요 실패, 그들은 모두이 같은 형태 진행 동일 모델 올 다른 모델의 다른 필드가 있습니다 progress_attachment 여러 이미지를 업로드하는 데 사용합니다. (나는 carrierwave 사용하고, exemple 등 SSR solution) 그것은 titledatecontentmain image 작동레일 및 carrierwave 업로드 여러 이미지가 나는 제목, 내용하는 main_image로, 일부 기사를 쓸 수있는 형태가

!

하지만 여러 파일 업로드가있는 필드는 하나의 이미지를 허용합니다 ... 여러 번 받아 들여야합니다 ... 무엇이 잘못 되었나요?

= simple_form_for(@progress, html: { multipart: true}) do |f| 
    =f.input :title 
    =f.input :date 
    =f.input :content 
    =f.input :main_image 
    =f.simple_fields_for :progress_attachments do |f| 
    =f.input :image, multiple: true, name: "progress_attachments_attributes[:image][]" 
    =f.button :submit 

내 모델 :

progress.rb

class Progress < ActiveRecord::Base 
    default_scope ->{order(created_at: :DESC)} 
    mount_uploader :main_image, MainImageUploader 
    has_many :progress_attachments 
    accepts_nested_attributes_for :progress_attachments 

    validates :main_image, presence: true 
    validates :title, presence: true 
    validates :content, presence: true 
    validates :date, presence: true 
end 

progress_attachment.rb

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

내 컨트롤러 :,536 그래서 여기

은 내 양식입니다progresses_controller.rb

class ProgressesController < ApplicationController 

    def index 
    @progresses = Progress.all 
    end 

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

    def new 
    @progress = Progress.new 
    @progress_attachment = @progress.progress_attachments.build 
    end 

    def create 
    @progress = Progress.new(progress_params) 

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

    def update 
    respond_to do |format| 
     if @progress.update(article_params) 
     format.html { redirect_to @progress, notice: 'Article was successfully updated.' } 
     format.json { render :show, status: :ok, location: @progress } 
     else 
     format.html { render :edit } 
     format.json { render json: @progress.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @progress.destroy 
    respond_to do |format| 
     format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 
    private 
    def progress_params 
     params.require(:progress).permit(:title, :content, :date, :main_image, progress_attachments_attributes: [:id, :progress_id, :image]) 
    end 

end 

progress_attachements_controller.rb

class ProgressAttachmentsController < ApplicationController 
    before_action :set_progress_attachment, only: [:show, :edit, :update, :destroy] 

    def index 
    @progress_attachments = ProgressAttachment.all 
    end 

    def new 
    @progress_attachment = ProgressAttachment.new 
    end 

    def create 
    @progress_attachment = ProgressAttachment.new(progress_attachment_params) 
    @progress_attachment = @progress.progress_attachments.build 
    respond_to do |format| 
     if @progress_attachment.save 
     format.html { redirect_to @progress_attachment, notice: 'Progress attachment was successfully created.' } 
     format.json { render :show, status: :created, location: @progress_attachment } 
     else 
     format.html { render :new } 
     format.json { render json: @progress_attachment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @progress_attachment.update(progress_attachment_params) 
     format.html { redirect_to @progress_attachment, notice: 'Progress attachment was successfully updated.' } 
     format.json { render :show, status: :ok, location: @progress_attachment } 
     else 
     format.html { render :edit } 
     format.json { render json: @progress_attachment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @progress_attachment.destroy 
    respond_to do |format| 
     format.html { redirect_to progress_attachments_url, notice: 'Progress attachment was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 

    def set_progress_attachment 
     @progress_attachment = ProgressAttachment.find(params[:id]) 
    end 

    def progress_attachment_params 
     params.require(:progress_attachment).permit(:progress_id, :image) 
    end 
end 

도움이 많이 덕분에 많이 주시면 감사하겠습니다!

답변

0

내 다중 이미지가 작동합니다.

나는 대체 :

=f.input :image, multiple: true, name: "progress_attachments_attributes[:image][]"

=f.input_field :image, multiple: true, name: "progress_attachments_attributes[:image][]"

에 의해