Я новичок в рельсах и пытаюсь реализовать загрузку изображений на ftp с помощью «несущей волны-ftp». Для загрузки изображений у меня есть два контроллера. Первый - это «event_controller», а второй - «events_pictures_controller».Ruby On Rails [Carrierwave]: Не удается удалить изображение
Фотографии загружаются на ftp. Но проблема в том, что когда я удаляю одну картинку, она уничтожает все событие. Пожалуйста помоги!
Вот моя События Модель:
class Event < ActiveRecord::Base
has_many :event_pictures, dependent: :destroy
accepts_nested_attributes_for :event_pictures, allow_destroy: true
validates_presence_of :name, :date
end
Вот мой EventPictures Модель:
class EventPicture < ActiveRecord::Base
mount_uploader :picture_title, EventPicturesUploader
validates_presence_of :picture_title
belongs_to :event, dependent: :destroy
end
События Контроллер:
def index
@events = Event.all.order('date DESC')
end
def show
@event_pictures = @event.event_pictures.all
end
def new
@event = Event.new
@event_picture = @event.event_pictures.build
end
def edit
end
def create
@event = Event.new(event_params)
respond_to do |format|
if @event.save
params[:event_pictures]['picture_title'].each do |a|
@event_picture = @event.event_pictures.create!(:picture_title => a, :event_id => @event.id)
end
format.html { redirect_to @event, notice: 'Event was successfully created.' }
else
format.html { render :new }
end
end
end
def destroy
@event = Event.find params[:id]
@event.destroy
redirect_to events_url
end
private
def set_event
@event = Event.find(params[:id])
end
def event_params
params.require(:event).permit(:name, :date, event_pictures_attributes: [:id, :event_id, :picture_title])
end
Это метод Destroy в EventPictures контроллер
def destroy
@event_picture = EventPicture.find params[:id]
@event_picture.destroy
redirect_to "events_url"
end
Между тем в events.show.html.erb, у меня есть это:
<% @event_pictures.each do |p| %>
<%= link_to image_tag(p.picture_title_url, :class => 'event-img'), image_path(p.picture_title_url) %>
<%= link_to 'Delete', p, method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
Просто попробуйте удалить 'depend:: destroy' из этого:' принадлежит_то: событие, зависимое:: destroy' в модели 'EventPicture'. – Deep