I’m playing around with the Postback action recipe listed in the recipes
book.
Here is the default code given:
def edit
@recipe = Recipe.find_by_id(params[:id]) || Recipe.new
if request.post?
@recipe.attributes = params[:recipe]
redirect_to :main_url and return if @recipe.save
end
end
Here is what my code sort of ended up looking like, altho i’ve ripped it
apart so many times now trying to figure it out:
def edit
begin
@entry = Entry.find(params[:id])
rescue
@entry = Entry.new
end
causes a weird error where on the second time in, with no params
passed in it still loads the previous entry object saved.
@entry = Entry.find_by_id(params[:id]) || Entry.new
logger.debug “!!! edit: #{@entry.inspect}”
if request.post?
@entry.attributes = params[:entry]
# pulled this out so i can inspect it
entrysaved = @entry.save
logger.debug "on save: #{@entry.inspect}"
redirect_to :action => "index" and return if entrysaved
end
end
I get some interesting results tho.
So, the first time i click the href (simple href to :action => “edit”)
the edit form pops up (blank), i type in the values and submit.
My page redirects and then loads the index page which shows me my entry
that i just saved.
All good so far.
Now i click the same href (no params attached or anything) and the form
pops back up, but this time its prefilled.
When i look at my inspect above, it looks like it loaded the last entry
saved. However if i inspect the params[:id] that shows nil.
The weirdest thing is that if i do
Entry.find_by_id(nil) in place of Entry.find_by_id(params[:id]) it still
loads the last entry saved. They have different addresses, so its not
the same object that was created and then passed around a session or
something.
So to top it off I tried using just what you see above, a
Entry.find(params[:id]) and did my error trapping around it.
That works. The old saved entry is never pulled back up.
So what in the world am I missing. Is there some ‘magic’ in rails that
is supposed to be a shortcut or am I just missing something in the code?
I can’t believe that it would be a bug, that would be caught immediately
by the team I’m sure.
Some System details:
Mac OSX 10.4.6
Ruby 1.8.4
Rails 1.1.2
Mysql 4.1.18
thx,
k