Redirecting no longer works

I have some truble redirecting my users to the previous page.

Here is an example update method in the movie controller.

def update @movie = Movie.find(params[:id]) if @movie.update_attributes(params[:movie]) flash[:notice] = "The given movie is now updated." end respond_with(@movie, location: :back) end 

I get this error.

undefined method 'hash_for_back_url' for #<Module:0x00000103eeaaa8> in the respond_with line.

I am using Rails 3.1 rc1 with Ruby 1.9.

It works when it does something like this.

respond_with(@movie, location: request.referer)

Does anyone know why the :back argument will not work?

+7
source share
3 answers

The only solution that worked for me was using request.referer .

Decision No. 1

 respond_with(@comment, location: request.referer) 

Decision number 2

 class ApplicationController < ActionController::Base helper_method :redirect_back def redirect_back(options = {}) if request.referer redirect_to request.referer, options else redirect_to root_path, options end end end # View / controller redirect_back notice: "Sending you back ... hopefully" 
+8
source

In this case, response_with expects: the location should be the result of a helper method of the URL, for example, in respond_with(@movie, :location => movie_url(@movie))

You should probably use one of these methods:

  • redirect to info page with redirect_to @movie , which is equivalent to redirect_to movie_path(@movie)

  • visualize the updated data form again with render :action => :edit (: edit represent the action used to display the form)

0
source

How about this:

 respond_with @movie do |format| format.html { redirect_to :back } end 

Allows you to override the default html responder so you can use redirect_to :back

0
source

All Articles