我有一个正在创建的画廊应用程序。我需要@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"