3

The update method is saving the data but the redirect is failing to render in the browser.

controller:

def update
  fbc = FbComments.find(params[:id])
  if fbc.update_attributes(params[:fb_comments])
    session[:report_for] = current_user
    redirect_to userhome_index_path
  end
end

console:

Rendered userhome/_feedback_receive_list.html.erb (2.2ms)
Rendered userhome/_applet.html.erb (1.2ms) ...
Rendered userhome/_feedback_give_list.html.erb (1.2ms)
Rendered userhome/index.rhtml within layouts/user_app (9.1ms)

As you can see, the console says the view is rendering including its partials, but in reality the browser remains on the page with the form. Same result in Safari, Firefox or Chrome. I seem to remember reading something about a Rails 3.0.8 issue not rendering a view on a redirect but i can't remember where or why. I am still researching... anybody know where the problem is?

Thank you.

NOTES:

I updated my controller with dpb's suggestion below. And then followed krohrbaugh and dpb's comments below which got rid of the 406 error. The controller code above is updated with their recommendations.

Experimenting, i changed the redirect url to pre-rest format but the outcome was the same so i changed it back. The data is still updated, but the redirect is still not rendering.

Jay
  • 6,206
  • 11
  • 48
  • 82
  • 1
    It doesn't seem like you're doing anything with the respond_to block. Have you tried it without it? What is the format being requested? [This question](http://stackoverflow.com/questions/1414446/weird-406-not-acceptable-error) may be relevant. – krohrbaugh Jun 14 '11 at 03:17
  • I agree with krohrbaugh. Try removing the respond_to block. – dpb Jun 14 '11 at 03:34
  • @krohrbaugh and @dpb, thanks. I took the respond_to out and the 406 is gone. The console says that the index page and its partials were rendered... but guess what... they are not. The page with the form remains. – Jay Jun 14 '11 at 04:01
  • jay, did u got any solution on this? – Swapnil Chincholkar Dec 10 '13 at 14:07
  • @SwapnilChincholkar, the answer that I checked below. – Jay Dec 10 '13 at 21:37

4 Answers4

3

I had the same issue an it turned out the form still had an old remote: true on it. I removed it and it was resolved.

Danny Santos
  • 1,090
  • 2
  • 10
  • 30
3

If you're going to specify a format, you need a respond_to block. This should work:

def update
  respond_to do |format|
    fbc = FbComments.find(params[:id])
    if fbc.update_attributes(params[:fb_comments])
      session[:report_for] = current_user
      format.html { redirect_to(root_path, :notice => 'Feedback was successfully updated.') }
    end
  end
end

Alternatively, if you only want to support html you can leave out the format.html all together like this:

def update
  fbc = FbComments.find(params[:id])
  if fbc.update_attributes(params[:fb_comments])
    session[:report_for] = current_user
    redirect_to(root_path, :notice => 'Feedback was successfully updated.')
  end
end

You will probably also want to add an else statement just in case the update fails. Then you can handle it gracefully by redirecting or rendering another view.

dpb
  • 671
  • 3
  • 8
  • thanks. This is really odd. I picked your first option and in the console it says the page this was redirected to was rendered. But it didn't happen in the browser. I restarted the server and same result. "Redirected to http://localhost:3000/ Completed 302 Found in 96ms". I'm checking on the 302 error. – Jay Jun 13 '11 at 22:43
  • Had the same problem. In my case the form I submitted was remote. – Al17 Jan 06 '16 at 13:55
1

I had the same problem with destroy. The controller generated by scaffold had a redirect_to rights_url for the object 'rights'. Rails server said Rendered ... 200 OK, but the page supposedly rendered did not appear in the browser. Hmmm.

So as a test I commented out the respond_to block and let the controller drop through. The server then said ActionView::MissingTemplate (Missing template rights/destroy with {:locale=>[:en, :en], :formats=>[:js, ... ], ... ).

Aha! It is looking for a :js response. So I created views/rights/destroy.js.erb containing this snippet of code:

alert("<%= flash[:msg] %>")
window.location.href="/"

The destroy controller stuffs a message into flash, which the alert displays. It then goes back to the home page. Seems to work fine.

Rather than just drop through, it is probably good form to clean up the controller to explicitly say

respond_to :js
Lex Lindsey
  • 521
  • 4
  • 4
0

I was trying to redirect to localhost:3000. adding http:// in front to make it http://localhost:3000 did the trick.

Ryan Odening
  • 21
  • 1
  • 2