1

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
Community
  • 1
  • 1
rosegrink
  • 333
  • 4
  • 9
  • CookieStore in rails is 4KB in size. The error that you get is raised whenever that limit is exceeded. I encourage you to check this [link](http://stackoverflow.com/questions/9473808/cookie-overflow-in-rails-application) out. Maybe it'll help – Tim Jul 21 '15 at 16:26

2 Answers2

1

Rather than doing a redirect, in this case the problem was solved by doing a render, then controlling the view by setting an instance var in the controller saying if it is the edit page or not. Also by using the update class in the CSS. However, this still has the the url for the show page, but at least the layout is correct.

rosegrink
  • 333
  • 4
  • 9
0

One way to do it would be to allow the edit action to accept the POST method as well. Use request.method to check whether it is a POST or GET, then perform your render or redirect accordingly.

fylooi
  • 3,840
  • 14
  • 24