I went to http://127.0.0.1:3000/bottles/new and I made a bottle
name- tod
size- 234
I then go to http://127.0.0.1:3000/bottles/0/edit
and I see the bottle there in the form
I then change tod to todd and click ‘save’
here’s the weird thing
I see the URL change to
http://127.0.0.1:3000/bottles/1
but i’d expect it to change to
http://127.0.0.1:3000/bottles/0
I’d expect the form to be submitted to /bottles/0 but it’s not, it’s submitted to /bottles/1
And the params have :id=1
So when it comes to my update, I have to write this line in the controller, subtracting 1 from the id
@abottle=Bottle.all[params[:id].to_i-1]
I can see that there is a difference of definition of :id that one could use… The id in the URL could be either
A)The index in the array e.g. Bottle.all[params[:id]] (Which is what i’ve got for the edit action). or
B) The id attribute of the object, (Which is what i’ve got for the update action @abottle=Bottle.all[params[:id].to_i-1]
One could say, it’s 'A', the URL. But then why should the update URL have an :id that is one up from the ID in the edit URL?
Thanks
On Fri, Jun 8, 2018 at 12:54 PM, Robert Phillips
<robert.p...@gmail.com> wrote:
> def edit
> # http://127.0.0.1:3000/bottles/0/edit :id==0
Where in the world are you getting this id of 0 ? What database
are you using? Every DB I've worked with starts an auto-increment
sequence with a positive (e.g. 1 or higher) number...
why is id off by one or defined different in edit vs in update? I will explain what I meanI have a controller called Bottles and a model BottleMy config\routes.rb file has the line- resources :bottlesI have this controllerclass BottlesController < ApplicationControllerdef indexenddef showenddef edit# http://127.0.0.1:3000/bottles/0/edit :id==0# <ActionController::Parameters {"controller"=>"bottles", "action"=>"edit", "id"=>"0"} permitted: false># Bottle.all[0] gives <Bottle id: 1, name: "tod", size: 234,...>@bottle=Bottle.all[params[:id].to_i] #:id==0
endI have these viewsC:\rubytest\testoby>type app\views\bottles\new.html.erb<%= form_for @bottle do |f| %><%= f.text_field :name %><%= f.text_field :size %><%= f.submit “Create” %><% end %>C:\rubytest\testoby>type app\views\bottles\edit.html.erb<%= form_for @bottle do |f| %><%= f.text_field :name %><%= f.text_field :size %><%= f.submit “Save” %><% end %>C:\rubytest\testoby>
I went to http://127.0.0.1:3000/bottles/new and I made a bottle
name- tod
size- 234
I then go to http://127.0.0.1:3000/bottles/0/edit
and I see the bottle there in the form
I then change tod to todd and click ‘save’
here’s the weird thing
I see the URL change to
http://127.0.0.1:3000/bottles/1
but i’d expect it to change to
http://127.0.0.1:3000/bottles/0
I’d expect the form to be submitted to /bottles/0 but it’s not, it’s submitted to /bottles/1
And the params have :id=1
So when it comes to my update, I have to write this line in the controller, subtracting 1 from the id
@abottle=Bottle.all[params[:id].to_i-1]
I can see that there is a difference of definition of :id that one could use… The id in the URL could be either
A)The index in the array e.g. Bottle.all[params[:id]] (Which is what i’ve got for the edit action). or
B) The id attribute of the object, (Which is what i’ve got for the update action @abottle=Bottle.all[params[:id].to_i-1]
One could say, it’s 'A', the URL. But then why should the update URL have an :id that is one up from the ID in the edit URL?
Thanks
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3e864d52-d02b-4aa4-9839-ba550b472fb0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<snip>
@bottle=Bottle.all[params[:id].to_i] #:id==0
Your error is on these lines. ..<snip>
In the edit action, you should be getting the id of the bottle so you can find the bottle usingBottle.find(params[:id])
For some weird reason, @bottle here should be defined in the edit action in your controller. But youredit action is empty.