Search code examples
ruby-on-rails-5

redirects and renders not sending browser anywhere in create and update functions


I'm trying to redirect to the #show action after creating or updating a foo item, but it seems like the page won't take redirect or render requests. All the code runs when I step through it, and I even get a success message:

Redirected to http://localhost:3000/foos/fake_id_13579

Still, the #show page won't load, and even the render 'edit' command won't simply refresh the page and show my error-message flash.

I should note that my index page does take render requests as it should, so it seems like the problem is only present on the 'edit' and 'new' pages.

I've tried using redirect_to @foo and foo_path. I've also ensured that foo_path returns the same URL as @foo in this line. Similarly, I've tried these commands with and without the 'return' statements which ensure that these commands are the last line of code in the function.

def update
  @foo = foo.find_by(token: params[:id])

  if @foo.update(foo_params)
    redirect_to foo_path
    #redirect_to @foo
    return
  else
    render 'edit'
    return
  end
end

In the if statement, I expect to be redirected to the show.html.erb page, but the screen does not move at all. In the else statement, I would expect my edit.html.erb page to be reloaded with appropriate flash messages at the top of the page, but although that code is being stepped through properly, no flash message comes up. I believe this is because the render request and the redirect request is not actually sending my webpage anywhere.


Solution

  • Check the form's remote or local attributes. remote should be false or local should be true