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
Community
  • 1
  • 1
user938363
  • 9,990
  • 38
  • 137
  • 303
  • 6
    use <%= link_to "Back", :back %> it should work – benoitr Sep 29 '11 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 '11 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 '11 at 23:12
  • see revised question with addition of the controller. – user938363 Sep 30 '11 at 01: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 '11 at 09:17
  • How to break up the dead loop? the back button should behave in such a way like this: if a user did p1 => p2 => p3. 'back' on p3 should go to p2. Now if the user hits p3 and, 'back' on p3. it should go to p2 (no problem now). BUT the 'back' on p2 should go to p1, INSTEAD of going to p3 and forming a dead loop. – user938363 Sep 30 '11 at 22:42
  • You could maybe simply use something like <%= link_to "Back", p2_path %> on your p3 view and <%= link_to "Back", p1_path %> on the p2 view. – benoitr Oct 03 '11 at 10:20

1 Answers1

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.

sealocal
  • 10,897
  • 3
  • 37
  • 50