I am completely new to rails and playing with the code to make pages work.
The link localhost:3000/zombies/1 works (show action)
but localhost:3000/zombies (index action) doesn’t. Below are my routes and controller:
ROUTES ARE:
resources :zombies
CONTROLLER is:
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
Why is this?
Editing answer based on the comment
The url,
localhost:3000/zombieswhich callsindexaction does not includeidparameter.That’s why the app is failing at
@zombie=Zombie.find(params[:id]).If you want to fix this issue, use
before_filteronshowaction only.before_filter :get_zombie_params, only: :showAnd insert this into index action as I have originally suggested.