Saturday, 24 August 2013

Stay on the same page after edit

Stay on the same page after edit

I'm trying to follow the RailsTutorial guide, but by doing my own
application instead. I have trouble with section 7, with the forms.
My controller :
def update
d = Deck.find(params[:id])
d.title = params[:deck][:title]
d.slug = params[:deck][:slug]
d.category = params[:deck][:category]
if d.save
redirect_to deck_path(d), notice: "Deck saved successfully"
else
render :edit
end
end
I know it's very, very far from good code, but I will refactor it later
(if you have a suggestion I'm all ears, but I use Rails 3, so I guess the
strong parameters of Rails 4 are out).
The problem is when the d.save does not work (due to validation), with the
render :edit.
Right now, when I enter invalid data, it tries to redirect to the show
action, and crashes because it does not have any data to display.
If I add @deck = d above the render, it works, but the url still is the
show action.
If my validation fail, how can I stay on the same URL and display my error
messages ? Is the "change URL but render the same page" behavior accepted
as valid ?
Thanks !
If you're interested in looking at the rest of the code, it's here :
https://github.com/cosmo0/TeachMTG/tree/remodel-decks

No comments:

Post a Comment