When there was an error on updating my model, I was rendering :edit, but this was stripping the /edit
from my url because #update
is the same as #show
with a different request method. To solve this I tried following the advice given here, but this caused me to get an ActionDispatch::Cookies::CookieOverflow
error when I try to submit an invalid form. How should I correctly re render the edit page, while keeping both the /edit
url and the flash messages? Is it possible to check for validity and show the errors without making a call to update?
Original code:
def edit
end
def update
respond_to do |format|
format.html do
if @model.update(model_params)
redirect_to home_base_url_or_default(model_url(@model)), notice: "Successfully updated."
else
render :edit
end
end
end
end
Failing code:
def edit
if flash[:model]
@model = flash[:model]
end
end
def update
respond_to do |format|
format.html do
if @model.update(model_params)
redirect_to home_base_url_or_default(model_url(@model)), notice: "Successfully updated."
else
flash[:model] = @model
redirect_to :action => :edit
end
end
end
end