I’m trying to set up a simple edit feature for an object in one of my tables. Here is my current code which I’ve seen online and in books but it doesn’t work
def edit
@fire_chief = FireChief.find(params[:id])
end
All I have to do is figure out how to edit a specific Fire Chief in list view and I can keep programming, but I’m stuck.
The error I’m recieving is “Couldn’t find FireChief without an ID”
If I use this little snippet of code it finds the edit form fine
def edit
@fire_chief = FireChief.last
end
But it always pulls up the last entry to be edited. I need it to pull the entry that i click…might be the 1st, 3rd, or 5th, etc. I know a lot of this is redundant, but I just want to be clear on what I’m trying to do.
This is my routes file:
resource :timesheet do
resource :command_officer
resources :fire_chief
resources :fire_fighters
resource :safety_officer
resources :emts
resources :hazmat_specialists
resources :command_vehicles
resources :engines
resources :emergency_supports
resources :hazmat_units
resources :field_units
resources :pumpers
resources :tankers
resources :rescue_units
end
end
I just changed the resource :fire_chief to be plural, so now its this resources :fire_chief
But I’m getting this error now:
Routing Error
uninitialized constant FireChiefController
To get an
idor any other data you need fromparamsyou have to pass the data inside the url like this example:/firechief/2/editThis way your
editmethod will know the id and the method will work. You can set this manually inside your routes, making a route like this:Or if you are using
resourcesinside your routes, it shall work by default.If you actually does not want to pass any data inside your url, you could implement a session based solution. Create a method to store the
idof thefirechiefinside a controller that fits your needs, or just add this line if you want to create the session inside a method (likefirechief#create) you already has:Now on your controller you can do this: