实例变量找不到id

时间:2013-03-12 00:43:52

标签: ruby-on-rails ruby-on-rails-3 rails-activerecord

我有一个我正在创建的图库应用程序。我需要在类的索引操作上引用实例变量@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

Index.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"

2 个答案:

答案 0 :(得分:0)

如果您想使用它,即使数据库中没有图像,那么您必须执行find_by_id

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

返回Nil而不是错误。

答案 1 :(得分:0)

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

@images = Image.all