3
<%= link_to_function "Back", "history.back()" %>

The above does not work in new and edit.html.erb. The back link points to itself (/new# or /edit#) so it does not really go anywhere back. However the back button works in index.html.erb.

See another stackoverflow post on back button: 'Back' browser action in Ruby on Rails

Any thoughts? Thanks.

PS: Category controller:

class CategoriesController < ApplicationController
  before_filter :require_signin
  before_filter :require_employee


  def index
    if session[:eng_dh]
      @categories = Category.all
    else
      @categories = Category.active_cate.all
    end

  end

  def new
    @category = Category.new

  end

  def create
    if session[:eng_dh] 
      #dynamic attr_accessible
      @category = Category.new(params[:category], :as => :roles_new)    

      if @category.save
        #@categories = Category.all
        redirect_to categories_path, :notice => 'Category was successfully created.' 

        # send out email
        #NotifyMailer.new_prod_email(@product).deliver
      else
        render :action => "new" 
      end
    end
  end

  def edit
    @category = Category.find(params[:id])
  end

  def update
    @category = Category.find(params[:id])
    #@category.reload. caused nil.reload error

    if @category.update_attributes(params[:category], :as => :roles_update)  
      #@category = Category.find(params[:id])

      redirect_to categories_path, :notice => 'Category was successfully updated'
    else
      @categories = Category.all
      render 'index'
    end 
  end

  def show
    @category = Category.find[params[:id]]
  end

end
7
  • 6
    use <%= link_to "Back", :back %> it should work
    – benoitr
    Sep 29, 2011 at 22:44
  • 1
    link_to 'Back', :back works. However it is getting into a dead loop between index and, for example, edit page, because the 'Back' button on index page does not point to the previous page before hitting the index page, instead it is pointing back to the edit page again. So you can easily getting into a dead loop between index and edit. Any ideas?
    – user938363
    Sep 29, 2011 at 22:55
  • Could you provide your controller in order to see how your actions are redirected. If I get it, you are redirected to the index page after editing, is it right?
    – benoitr
    Sep 29, 2011 at 23:12
  • see revised question with addition of the controller.
    – user938363
    Sep 30, 2011 at 1:28
  • 1
    So it seems your back link behave as it has to do. After editing your page you are redirected to the index page; Then if you hit the back link on the index page after this it will render the last page (so the edit page). If you want your link performs in another way, please explain what you want to achieve; maybe a conditional loop will do the job.
    – benoitr
    Sep 30, 2011 at 9:17

1 Answer 1

12

A 'Back' link that satisfies most Rails applications' needs is to use the link_to helper:

<%= link_to "Back", :back %>

The :back Symbol option is even noted in the Rails API docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.