Я реализовал несколько загрузок с помощью Carrierwave, и я не могу правильно обновить сообщение. Вот мой контроллер:Rails 4 Carrierwave, после обновления нескольких загрузок не работает корректно.
class PostsController < ApplicationController
before_action :find_posts, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show, :home]
def home
end
def index
if params[:category].blank?
@posts = Post.all.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@posts = Post.where(category_id: @category_id).order("created_at DESC")
end
end
def show
@inquiries = Inquiry.where(post_id: @post).order("created_at DESC")
@random_post = Post.where.not(id: @post).order("RANDOM()").first
@post_attachments = @post.post_attachments.all
end
def new
@post = current_user.posts.build
@post_attachment = @post.post_attachments.build
end
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
params[:post_attachments]['image'].each do |a|
@post_attachment = @post.post_attachments.create!(:image => a)
end
format.html { redirect_to @post, notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
def update
if @post.update(post_params)
flash[:notice] = "Post successfully updated!"
redirect_to @post
else
flash[:notice] = "Something went wrong...give it another shot!"
render 'edit'
end
end
def edit
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
def upvote
@post.upvote_by current_user
redirect_to @post
end
def downvote
@post.downvote_by current_user
redirect_to @post
end
private
def find_posts
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :price, :description, :location, :category_name, :contact_number, :image)
end
end
В основном, пост создает просто отлично, но когда я пытаюсь обновить образы, новые образы не заменяют старые. Я не уверен, как изменить свое обновление, чтобы оно принимало новые изображения. ПРИМЕЧАНИЕ. Другие поля работают, когда я обновляю, а не мои изображения.
Что я могу изменить в своем контроллере, чтобы заставить его работать? Если вам нужно увидеть мою форму или что-то еще, сообщите мне, но я вполне уверен, что это в контроллере.
Я знаю это, и я не совсем уверен, как добавить это к текущему коду. –
Вы используете form_for в своем файле вида? или form_tag? В [carrierwave github] (https://github.com/carrierwaveuploader/carrierwave#user-content-multiple-file-uploads) говорится, что там используется '<% = form.file_field: аватары, несколько: true%>' в который я ожидаю в вашей форме, у вас будет что-то вроде '<% = f.file_field: вложения, multiple: true%>'. Но если вы не выполнили свое соглашение о множественном загрузке нескольких файлов и создали другую таблицу 'post_attachments', вам нужно было бы изменить ваши файлы просмотра, чтобы включить: id, и чтобы вы могли обновлять эти вложения. –
Я буду обновлять свой ответ в возможных изменениях в вашем контроллере, когда я буду свободен позже. –