1

我有一个正在创建的画廊应用程序。我需要@image在类的索引操作上引用实例变量。但是,当我在类中声明它时,它会导致Couldn't find Image without an ID. 我该如何解决这个错误。

class Admin::ImagesController < ApplicationController
    before_filter :get_album, :only => [:show, :index, :destroy]
    def index
        @images = Image.all
        @image = Image.find(params[:image_id])
    end
    def new
        @album = Album.find(params[:album_id])
        @image = Image.new(params[:image_id])
    end
    def create
        @image = Image.new(params[:image])
        if @image.save
            flash[:notice] = "Successfully added image!"
            redirect_to [:admin, :albums]
        else
            render :action => 'new'
        end
    end
    def show
        @image = Image.find(params[:id])
    end
    def destroy
        @image = Image.find(params[:image_id])
        @image.destroy
        redirect_to admin_albums_path
    end
    private
    def get_album
        @album = Album.find(params[:album_id])
    end

end

索引.html.erb

<% @images.each do |image|%>
    <%= image.title %>
    <%= image.description %>
    <%= image.image_name %>
    <%= button_to "Delete", @image, :method => :delete, :style => "display: block;" %>
    <%= debug @image %>
<% end %>

图式

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130312005119) do

  create_table "albums", :force => true do |t|
    t.string   "title",       :null => false
    t.text     "description"
    t.date     "date"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "images", :force => true do |t|
    t.string   "title"
    t.string   "description"
    t.string   "image_name"
    t.datetime "date"
    t.integer  "album_id"
    t.integer  "image_id"
  end

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

end

路线

Admin::Application.routes.draw do
  get "albums/index"

  get "dashboard/index"

  namespace :admin, :shallow => true do
    root :to => "dashboard#index"
    resources :dashboard
    resources :albums do
      resources :images
     end
    get "admin/album"
    end
    get "logout" => "sessions#destroy", :as => "logout"
  get "login" => "sessions#new", :as => "login"
  get "signup" => "users#new", :as => "signup"
    # resources :users
  resources :basic
    root :to => "basic#index"
4

2 回答 2

0

如果你想使用它,即使图像不存在于数据库中,那么你必须做 find_by_id

@image = Image.find_by_id(params[:image_id])

这将返回 Nil 而不是错误。

于 2013-03-12T01:59:22.573 回答
0

为什么在 Image 模型中使用 image_id 列。创建模型时会自动生成 id 列。从您的 Image 模型中删除 image_id 并在您的图像控制器索引中使用

@images = Image.all

于 2013-03-12T04:32:57.733 回答