Rubytastic
Rubytastic

Reputation: 15501

render a page that is only requestable 1 time?

How would one render a page say a "/logout" page that displays a "Thanks for visiting us" then if the user reloads the browser it would load "/" instead?

I have a

match "/logout" => "home#logout"

But don't wan't anyone that requesting "/logout" will see this page, it should only be rendered direct after the user is signed_out.

What would be the best way to approach this render a view dependant with a conditional redirect ( to root_path) instead of using a redirect_to

Upvotes: 2

Views: 77

Answers (1)

James
James

Reputation: 4737

You probably want:

match '/logout' => 'sessions#destroy', :via => :delete

And use logout_path in your link_to helper or however you decide to implement logging out in your application.

And write your message in the flash in SessionsController#destroy. It may look something like:

class SessionsController < ApplicationController
  def destroy
    sign_out # or whatever you named your method for signing out
    flash[:notice] = "Thanks for visiting us"
    redirect_to root_path
  end
end

In order to make sure the request goes to root_path when the user is not signed in, you should place a before_filter in ApplicationController:

class ApplicationController < ActionController::Base
  before_filter :authenticate_user

  def authenticate_user
    unless signed_in?
      redirect_to root_path
    end
  end
  helper_method :authenticate_user
end

This way, after the user is signed out, all requests will redirect to root_path.

To allow requests to pages without being signed in, use skip_before_filter in the appropriate controller classes:

def MyPublicStuffsController < ApplicationController
  skip_before_filter :authenticate_user
  # ...
end

Upvotes: 3

Related Questions