ruby on rails - Stay on the same page after edit -
i'm trying follow railstutorial guide, doing own application instead. have trouble section 7, 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, far code, refactor later (if have suggestion i'm ears, use rails 3, guess strong parameters of rails 4 out).
the problem when d.save
not work (due validation), render :edit
.
right now, when enter invalid data, tries redirect show
action, , crashes because not have data display.
if add @deck = d
above render
, works, url still show
action.
if validation fail, how can stay on same url , display error messages ? "change url render same page" behavior accepted valid ?
thanks !
if you're interested in looking @ rest of code, it's here : https://github.com/cosmo0/teachmtg/tree/remodel-decks
actually when fails url not 'show' 'update' url.
your code works.
when submit form, browser sends post request controller#update.
when update fails, tell update action "render :edit". renders :edit action inside :update route.
the update route uses same url show action : can check when running 'rake routes', difference method post 'update' vs 'show'
that's why think show url in browser works: on update action renders :edit.
(telling update action 'render :edit' doesn't mean redirected :edit :update)
is clear enough ?
Comments
Post a Comment