复选框用于删除带有载波的表单中的图像

时间:2017-04-25 14:59:51

标签: ruby-on-rails forms carrierwave

我在ruby on rails上创建一个博客应用程序。我有#34;专辑"模特和照片"模型。在相册编辑视图中,我希望能够删除与相册相关联的上传图像。 不幸的是,我为复选框收到了一个未定义的方法错误?!

您是否有任何解决方案或提示可以使其发挥作用?

如何才能找到无效的原因?

如果您需要进一步的信息,请告诉我。

enter image description here

相册/ edit.html.erb

<%= form_for @album do |f| %>
  <% if @album.errors.any? %>
  <h2><%= pluralize(@album.errors.count, "error") %> prevent this post from saving:</h2>
  <ul>
    <% @album.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>k
  </ul>
  <% end %>

  <div class="blog_edit_wrapper">

    <%= f.label :title %><br>
    <%= f.text_field :title, class: "blog_edit_title" %>

    <br>
    <br>
    <%= f.submit 'Submit', class: "blog_edit_submit" %>
  </div>

<% end %>
<hr>

<% @album.photos.each do |photo| %>
  <%= image_tag(photo.gallery_image) %>
  <%= photo.check_box :remove_gallery_image %>
  <br>
  <br>
  <%= f.submit 'Submit', class: "blog_edit_submit" %>
<% end %>

photos_controller.rb

class PhotosController < ApplicationController
  before_action :find_photo, only: [ :show, :edit, :update, :destroy]
  before_action :set_album

  def index
    @photo = Photo.all
  end

  def new
    @photo = @album.photos.new
  end

  def show
  end

  def create
    @photo = @album.photos.new photo_params
    @photo.album = @album
    if @photo.save
      redirect_to @album
    else
    render :new
    end
  end

  def edit
  end

  def update
    @photo = @album.photos.find params[:id]

    if @photo.update photo_params
      redirect_to @album, notice: "Your photo was successfully saved!"
    else
      render 'edit'
    end
  end

  def destroy
    @photo.destroy
    redirect_to photos_path
  end

  private

  def set_album
    @album = Album.find params[:album_id]
  end

  def photo_params
    params.require(:photo).permit(:gallery_image, :album_id, :title, :remove_gallery_image)
  end

  def find_photo
    @photo = Photo.find(params[:id])
  end
end

photo.rb

class Photo < ActiveRecord::Base
  mount_uploader :gallery_image, ImageUploader
  belongs_to :album
end

albums_controller.rb

class AlbumsController < ApplicationController
  before_action :find_album, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @albums = Album.all.order("created_at desc").paginate(page: params[:page], per_page: 12)
  end

  def new
    @album = Album.new
  end

  def show
    @photo = Photo.all
  end

  def create
    @album = Album.new album_params
    if @album.save
      redirect_to @album
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @album.update album_params
      redirect_to @album, notice: "Your article was successfully saved!"
    else
      render 'edit'
    end
  end

  def destroy
    @album.destroy
    redirect_to albums_path
  end

  private

  def album_params
    params.require(:album).permit(:title)
  end

  def find_album
    @album = Album.find(params[:id])
  end
end

album.rb

class Album < ActiveRecord::Base
  has_many :photos
end

schema.rb

ActiveRecord::Schema.define(version: 20170424131600) do

  create_table "albums", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "friendly_id_slugs", force: :cascade do |t|
    t.string   "slug",                      null: false
    t.integer  "sluggable_id",              null: false
    t.string   "sluggable_type", limit: 50
    t.string   "scope"
    t.datetime "created_at"
  end

  add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
  add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
  add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
  add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"

  create_table "photos", force: :cascade do |t|
    t.string   "gallery_image"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "album_id"
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "content"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "slug"
    t.string   "post_main_image"
  end

  add_index "posts", ["slug"], name: "index_posts_on_slug", unique: true

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

1 个答案:

答案 0 :(得分:0)

我相信您的问题是您不使用表单而只是迭代照片。

此外,您可以将用于编辑照片的代码放在第一个表单中。

将此代码放在<br>代码下方,然后移除<% @album.photos.each do |photo| %>互动。

<%= fields_for(@album, @album.photos.build) do |f| %> <%= image_tag(f.gallery_image)%> <%= f.check_box :remove_gallery_image %> <% end %>

修改 澄清以上内容;将上述代码放在原始编辑@album表单中,并删除@album.photos.each

的迭代