【发布时间】:2013-03-12 00:43:51
【问题描述】:
我正在创建一个画廊应用程序。我需要在类的索引操作上引用实例变量@image。但是,当我在 Class 中声明它时,它会导致 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"
【问题讨论】:
-
显示该错误是因为
params[:image_id]为零,您能否向我们显示参数(请参阅服务器日志) -
如果您访问的是
params[:image_id],您需要确保image_id存在。因此,例如,?image_id=1应该附加到您的图片 URL 中。 -
它在模式中。我应该在“图像创建表单”上添加 hidden_field_tag 吗?
-
顺便说一句,当我做
@image = Image.find(params[:id])时出现同样的错误@ -
在控制台中,输入
rake routes。将输出粘贴到原始提交中。
标签: ruby-on-rails ruby-on-rails-3 rails-activerecord