2017-04-25 31 views
0

我在rails上創建一個博客應用程序。我有「專輯」模型和「照片」模型。在專輯編輯視圖中,我希望能夠刪除與專輯關聯的上傳圖像。 不幸的是,我收到一個未定義的方法錯誤複選框?用於刪除載波形式的圖像的複選框

你有任何解決方案或提示,使這項工作?

我怎樣才能找到爲什麼它不起作用的底部?

如果您需要更多信息,請告訴我。

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 

回答

0

我相信你的問題是你沒有使用表格,只是迭代照片。

此外,您可以將編輯照片的代碼放在第一個窗體中。

將此代碼置於<br>標記的下方並將其除去<% @album.photos.each do |photo| %> interation。

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

編輯: 爲了闡明以上;將上述代碼放在原始編輯@album表單中,並刪除迭代的@album.photos.each