ฉันยังใหม่กับ Rail และเล่นกับโค้ดเพื่อทำให้เพจทำงานได้ ลิงก์ localhost:3000/zombies/1 ใช้งานได้ (แสดงการทำงาน) แต่ localhost:3000/zombies (การดำเนินการดัชนี) ใช้งานไม่ได้ ด้านล่างนี้คือเส้นทางและตัวควบคุมของฉัน:
เส้นทางคือ: ทรัพยากร :ซอมบี้
ตัวควบคุมคือ:
class ZombiesController < ApplicationController
before_filter :get_zombie_params
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: @zombies }
end
end
def show
@disp_zombie = increase_age @zombie, 15
@zombie_new_age = @disp_zombie
respond_to do |format|
format.html # show.html.erb
format.json { render json: @zombie }
end
end
def increase_age zombie, incr
zombie = zombie.age + incr
end
def get_zombie_params
@zombie=Zombie.find(params[:id])
@zombies = Zombie.all
end
end
ทำไมเป็นเช่นนี้?
Zombie.find(params[:id])
และZombie.all
ได้ แต่ในการดำเนินการดัชนี คุณไม่มีพารามิเตอร์ ดังนั้นZombie.find(params[:id]
ของคุณจึงให้ข้อผิดพลาดActiveRecord
- person mehulkar   schedule 24.10.2012